//本题大意:找到所有增序和降序的三元素子序列
//题目链接:
//用树状数组计算i位置左边比他小的*右边比他大的
//加上左边比他大右边比他小的..就是ans
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int mn = 100000;
__int64 tree[mn+100],ldpmax[mn+100];
__int64 a[mn+100],ldpmin[mn+100],rdpmin[mn+100],rdpmax[mn+100];
int Lowbit(int x)
{
return x&(-x);
}
void Updata(int x)
{
for(int i = x ; i <= mn ; i += Lowbit(i))
tree[i]++;
}
__int64 Getsum(int x)
{
__int64 sum = 0;
while(x)
{
sum += tree[x];
x -= Lowbit(x);
}
return sum;
}
int main()
{
int test;
while(scanf("%d",&test) != EOF)
{
while(test--)
{
int n;
scanf("%d",&n);
memset(tree,0,sizeof(tree));
for(int i = 1 ; i <= n ; i++)
{
scanf("%d",&a[i]);
Updata(a[i]);
ldpmin[i] = Getsum(a[i]-1);
ldpmax[i] = i - 1 - ldpmin[i];
}
memset(tree,0,sizeof(tree));
for(int i = n ; i >= 1 ; i--)
{
Updata(a[i]);
rdpmin[i] = Getsum(a[i]-1);
rdpmax[i] = n - i - rdpmin[i];
}
__int64 ans = 0;
for(int i = 1 ; i <= n ; i++)
ans += ldpmin[i]*rdpmax[i] + ldpmax[i]*rdpmin[i];
printf("%I64d\n",ans);
}
}
}
//题目链接:
//用树状数组计算i位置左边比他小的*右边比他大的
//加上左边比他大右边比他小的..就是ans
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int mn = 100000;
__int64 tree[mn+100],ldpmax[mn+100];
__int64 a[mn+100],ldpmin[mn+100],rdpmin[mn+100],rdpmax[mn+100];
int Lowbit(int x)
{
return x&(-x);
}
void Updata(int x)
{
for(int i = x ; i <= mn ; i += Lowbit(i))
tree[i]++;
}
__int64 Getsum(int x)
{
__int64 sum = 0;
while(x)
{
sum += tree[x];
x -= Lowbit(x);
}
return sum;
}
int main()
{
int test;
while(scanf("%d",&test) != EOF)
{
while(test--)
{
int n;
scanf("%d",&n);
memset(tree,0,sizeof(tree));
for(int i = 1 ; i <= n ; i++)
{
scanf("%d",&a[i]);
Updata(a[i]);
ldpmin[i] = Getsum(a[i]-1);
ldpmax[i] = i - 1 - ldpmin[i];
}
memset(tree,0,sizeof(tree));
for(int i = n ; i >= 1 ; i--)
{
Updata(a[i]);
rdpmin[i] = Getsum(a[i]-1);
rdpmax[i] = n - i - rdpmin[i];
}
__int64 ans = 0;
for(int i = 1 ; i <= n ; i++)
ans += ldpmin[i]*rdpmax[i] + ldpmax[i]*rdpmin[i];
printf("%I64d\n",ans);
}
}
}