1 #include<iostream>
2 #include<string>
3 #include<algorithm>
4 #include<cstdlib>
5 #include<cstdio>
6 #include<set>
7 #include<map>
8 #include<vector>
9 #include<cstring>
10 #include<stack>
11 #include<cmath>
12 #include<queue>
13 #include <bits/stdc++.h>
14 using namespace std;
15 #define INF 0x3f3f3f3f
16 #define ll long long
17 #define clc(a,b) memset(a,b,sizeof(a))
18
19 inline int lowbit(int x)
20 {
21 return x&-x;
22 }
23
24 struct bit
25 {
26 int n;
27 vector<int>C;
28
29 void resise(int n)
30 {
31 this->n=n;
32 C.resize(n);
33 }
34 void clear()
35 {
36 fill(C.begin(),C.end(),0);
37 }
38 int sum(int x)
39 {
40 int ret=0;
41 while(x>0)
42 {
43 ret+=C[x];
44 x-=lowbit(x);
45 }
46 return ret;
47 }
48 void add(int x,int d)
49 {
50 while(x<=n)
51 {
52 C[x]+=d;
53 x+=lowbit(x);
54 }
55 }
56 };
57
58 const int maxn=20000+5;
59 int n,a[maxn],c[maxn],d[maxn];
60
61 bit f;
62
63 int main()
64 {
65 int t;
66 scanf("%d",&t);
67 while(t--)
68 {
69 scanf("%d",&n);
70 int maxa=0;
71 for(int i=1; i<=n; i++)
72 {
73 scanf("%d",&a[i]);
74 maxa=max(maxa,a[i]);
75 }
76 f.resise(maxa);
77 f.clear();
78 for(int i=1; i<=n; i++)
79 {
80 f.add(a[i],1);
81 c[i]=f.sum(a[i]-1);
82 }
83 f.clear();
84 for(int i=n; i>=1; i--)
85 {
86 f.add(a[i],1);
87 d[i]=f.sum(a[i]-1);
88 }
89 ll ans=0;
90 for(int i=1; i<=n; i++)
91 ans+=(ll)c[i]*(n-i-d[i])+(ll)d[i]*(i-c[i]-1);
92 printf("%lld
",ans);
93 }
94 return 0;
95 }