#include #include #include using namespace std; inline int lowbit(int x) { return x&-x; } struct FenwickTree { int n; int C[50010]; void resize(int n) { this->n = n; } void clear() { memset(C, 0, sizeof C); } int sum(int x) { int ret = 0; while(x > 0) { ret += C[x]; x -= lowbit(x); } return ret; } void add(int x, int d) { while(x <= n) { C[x] += d; x += lowbit(x); } } } f; int a[50010], n; long long aa[50010], bb[50010]; int main() { int T; scanf("%d", &T); while(T--) { scanf("%d", &n); f.resize(n); for(int i = 1; i <= n; ++i) { scanf("%d", &a[i]); } aa[0] = bb[n+1] = 0; f.clear(); long long ans = 0; for(int i = 1; i <= n; ++i) { aa[i] = f.sum(a[i] - 1) + aa[i-1]; f.add(a[i], 1); } f.clear(); int cnt = 0; for(int i = n; i >= 1; --i) { bb[i] = cnt - f.sum(a[i]); cnt++; f.add(a[i], 1); ans += 1LL * bb[i] * aa[i-1]; } cout << ans << endl; } return 0; }