#pragma GCC optimize("O3") #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; using P = pair; using T = tuple; template inline T chmax(T &a, const T b) {return a = (a < b) ? b : a;} template inline T chmin(T &a, const T b) {return a = (a > b) ? b : a;} constexpr int mod = 998244353; constexpr int MOD = 1e9 + 7; constexpr int inf = 1e9; constexpr long long INF = 1e18; #define all(a) (a).begin(), (a).end() int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; // Range Sum Query class SegmentTree{ public: int n; vector node; SegmentTree(vector v) { int sz = v.size(); n = 1; while(n < sz) n *= 2; node.resize(2*n-1, 0); for(int i=0; i=0; i--) node[i] = node[i*2+1] + node[i*2+2]; } void add(int x, ll val){ x += (n - 1); node[x] += val; while(0 < x){ x = (x - 1) / 2; node[x] = node[2*x+1] + node[2*x+2]; } } ll getsum(int a, int b, int k=0, int l=0, int r=-1){ if(r < 0) r = n; if(b <= l || r <= a) return 0; if(a <= l && r <= b) return node[k]; ll vl = getsum(a, b, 2*k+1, l, (l+r)/2); ll vr = getsum(a, b, 2*k+2, (l+r)/2, r); return vl + vr; } }; int main(){ cin.tie(0); ios::sync_with_stdio(false); int n; cin>>n; vector a(n); for(int i=0; i>a[i]; set st; for(int i=0; i num2idx; int idx = 0; for(auto i:st){ num2idx[i] = idx++; } SegmentTree clseg(vector(n, 0)), slseg(vector(n, 0)); vector cntl(n, 0), suml(n, 0); for(int i=0; i(n, 0)), srseg(vector(n, 0)); vector cntr(n, 0), sumr(n, 0); for(int i=n-1; i>=0; i--){ cntr[i] = crseg.getsum(0, num2idx[a[i]]); sumr[i] = srseg.getsum(0, num2idx[a[i]]); crseg.add(num2idx[a[i]], 1); srseg.add(num2idx[a[i]], a[i]); } ll ans = 0; for(int i=1; i+1