#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <queue> #include <algorithm> #include <vector> #include <cstring> #include <stack> #include <cctype> #include <utility> #include <map> #include <string> #include <climits> #include <set> #include <string> #include <sstream> #include <utility> #include <ctime> using std::priority_queue; using std::vector; using std::swap; using std::stack; using std::sort; using std::max; using std::min; using std::pair; using std::map; using std::string; using std::cin; using std::cout; using std::set; using std::queue; using std::string; using std::istringstream; using std::make_pair; using std::getline; using std::greater; using std::endl; typedef long long LL; typedef unsigned long long ULL; const int MAXN(20010); int arr[MAXN], tarr[MAXN]; //此处e为最后一个元素位置+1 int find(int tar, int s, int e) { int m; while(s < e) { m = s+(e-s)/2; if(tarr[m] >= tar) e = m; else s = m+1; } return s; } struct FENWICK { int ma[MAXN]; void init() { memset(ma, 0, sizeof(ma)); } int lowbit(int n) { return n&(-n); } void add(int ind, int val, int n) { for(int i = ind; i <= n; i += lowbit(i)) ma[i] += val; } int query(int ind) { int ret = 0; for(int i = ind; i >= 1; i -= lowbit(i)) ret += ma[i]; return ret; } }; FENWICK fw; int ls[MAXN], lb[MAXN], rs[MAXN], rb[MAXN]; int main() { int T; scanf("%d", &T); while(T--) { int n; scanf("%d", &n); for(int i = 1; i <= n; ++i) { scanf("%d", arr+i); tarr[i] = arr[i]; } sort(tarr+1, tarr+1+n); fw.init(); for(int i = 1; i <= n; ++i) { int temp = find(arr[i], 1, n+1); ls[i] = fw.query(temp-1); lb[i] = fw.query(n)-fw.query(temp); fw.add(temp, 1, n); } fw.init(); for(int i = n; i >= 1; --i) { int temp = find(arr[i], 1, n+1); rs[i] = fw.query(temp-1); rb[i] = fw.query(n)-fw.query(temp); fw.add(temp, 1, n); } LL ans = 0LL; for(int i = 1; i <= n; ++i) ans += (LL)ls[i]*rb[i]+(LL)lb[i]*rs[i]; printf("%lld\n", ans); } return 0; }