#include #include #include #include using namespace std; template int compressed(vector &a) { vector b = a; sort(begin(b), end(b)); b.erase(unique(begin(b), end(b)), end(b)); for (T &ai: a) ai = lower_bound(begin(b), end(b), ai) - begin(b); return b.size(); } template struct BIT { const Abel UNITY_SUM = 0; vector dat; BIT(int n) : dat(n, UNITY_SUM) { } // [0, n) inline void add(int i, Abel x) { while (i < dat.size()) { dat[i] += x; i |= i + 1; } } inline Abel sum(int i) { // [0, i] Abel res = UNITY_SUM; while (i >= 0) { res += dat[i]; i = (i & (i + 1)) - 1; } return res; } inline Abel sum(int a, int b) { return sum(b - 1) - sum(a - 1); } // [a, b) /* debug */ Abel operator[](int i) { return sum(i, i + 1); } void dump() { for (int i = 0; i < dat.size(); ++i) cout << sum(i, i + 1) << ","; cout << endl; } }; int main() { int n; cin >> n; vector a(n); for (int &ai: a) cin >> ai; int sz = compressed(a); BIT left(sz), right(sz); for (int ai: a) right.add(ai, 1); long long ans = 0, same = 0; for (int ai: a) { ans += left.sum(0, ai) * right.sum(0, ai); ans += left.sum(ai + 1, sz) * right.sum(ai + 1, sz); ans -= same - left[ai] * right[ai]; left.add(ai, 1); same += right[ai]; right.add(ai, -1); same -= left[ai]; } cout << ans << endl; return 0; }