#include #include #include #include class BitSum { public: BitSum(int n) : _n(n+1), _table(_n, 0), _origin(n, 0) {} int operator[](int item) const { return _origin[item]; } void add(int i, int x) { _origin[i] += x; i += 1; while (i < _n) { _table[i] += x; i += i & -i; } } int sum(int i) const { i += 1; int res = 0; while (i > 0) { res += _table[i]; i -= i & -i; } return res; } private: int _n; std::vector _table; std::vector _origin; }; int main() { int n; std::cin >> n; std::vector aa(n); for (int i = 0; i < n; i++) { std::cin >> aa[i]; } std::vector dec(aa); std::sort(dec.begin(), dec.end()); dec.erase(std::unique(dec.begin(), dec.end()), dec.end()); std::map enc; for (int i = 0; i < dec.size(); i++) { enc[dec[i]] = i; } for (int i = 0; i < n; i++) { aa[i] = enc[aa[i]]; } int ans = 0; for (int k = 0; k < 2; k++) { BitSum left(n), right(n), pair(n); for (int i = 0; i < n; i++) { right.add(aa[i], 1); } for (int i = 1; i < n-1; i++) { int a = aa[i-1]; int l = left[a]; int r = right[a]; pair.add(a, r-l-1); left.add(a, 1); right.add(a, -1); a = aa[i]; ans += left.sum(a-1) * right.sum(a-1) - pair.sum(a-1); } for (int i = 0; i < n; i++) { aa[i] = n-1 - aa[i]; } } std::cout << ans << std::endl; return 0; }