#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; typedef long long INT; const INT MAX_N = 100010; const INT N = 20010; INT nN; INT nNum[N]; INT nTree[MAX_N + 10]; INT nLeft[2][N], nRight[2][N];
INT LowBit(INT nI) { return nI & (-nI); }
void Add(INT nI, INT nAdd) { while (nI <= MAX_N) { nTree[nI] += nAdd; nI += LowBit(nI); } }
INT Query(INT nPos) { INT nAns = 0; while (nPos > 0) { nAns += nTree[nPos]; nPos -= LowBit(nPos); } return nAns; }
int main() { INT nT;
scanf("%I64d", &nT); while (nT--) { scanf("%I64d", &nN); memset(nTree, 0, sizeof(nTree)); for (INT i = 1; i <= nN; ++i) { scanf("%I64d", nNum[i]); nLeft[0][i] = Query(nNum[i]); nLeft[1][i] = Query(MAX_N) - Query(nNum[i] - 1); Add(nNum[i], 1); } memset(nTree, 0, sizeof(nTree)); for (INT i = nN; i >= 1; --i) { nRight[0][i] = Query(MAX_N) - Query(nNum[i] - 1); nRight[1][i] = Query(nNum[i]); Add(nNum[i], 1); } INT nAns = 0; for (INT i = 1; i <= nN; ++i) { nAns += nLeft[0][i] * nRight[0][i] + nLeft[1][i] * nRight[1][i]; } printf("%I64d\n", nAns); }