#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std ; using ll = long long ; using ld = long double ; template using V = vector ; template using VV = V> ; using pll = pair ; #define all(v) v.begin(),v.end() ll mod = 998244353 ; long double pie = acos(-1) ; ll INF = 1000000000000 ; void yorn(bool a){if(a) cout << "Yes" << endl ; else cout << "No" << endl ;} //void YorN(bool a){if(a) cout << "YES" << endl ; else cout << "NO" << endl ;} ll gcd(long long a,long long b){if(b==0) return a ; return gcd(b,a%b) ;} ll lcm(long long a,long long b){return a/gcd(a,b)*b ;} void fix_cout(){cout << fixed << setprecision(20) ;} template void chmax(T &a,T &b){if(a void chmin(T &a,T &b){if(a>b) a = b ;} template class SGT{ public : int n ; vector num ; function f ; T d ; // コンストラクタ:サイズ、関数、単位元を指定 SGT(size_t n_,function f_,T d_) : f(f_),d(d_){ n = 1 ; while(n(2*n-1,d) ; } // sz個のaで前から初期化 void init(int sz,T a){ for(int i=0;i=0;i--) num[i] = f(num[i*2+1],num[i*2+1]) ; } // 配列aで前から初期化 void init(vector &a){ for(int i=0;i=0;i--) num[i] = f(num[i*2+1],num[i*2+2]) ; } // 0-indexedでp番目をaで上書き void update(int p,T a){ p += n-1 ; num[p] = a ; while(p){ p = (p-1)/2 ; num[p] = f(num[p*2+1],num[p*2+2]) ; } } // 0-indexedでp番目にaを加算 void add(int p,T a){ p += n-1 ; num[p] += a ; while(p){ p = (p-1)/2 ; num[p] = f(num[p*2+1],num[p*2+2]) ; } } T query(int a,int b,int cur=0,int l=0,int r=-1){ if(r<0) r = n ; if(r<=a||b<=l) return d ; if(a<=l&&r<=b) return num[cur] ; T r1 = query(a,b,cur*2+1,l,(l+r)/2) ; T r2 = query(a,b,cur*2+2,(l+r)/2,r) ; return f(r1,r2) ; } T operator[](int p){ return num[p+n-1] ; } int bsr(int a,int b,T x,function check,int l=0,int r=-1,int cur=0){ if(r<0) r = n ; cout << l << " " << r << " " << cur << endl ; if(!check(num[cur],x)||r<=a||b<=l) return a-1 ; if(cur>=n-1) return cur-(n-1) ; int right = bsr(a,b,x,check,(l+r)/2,r,cur*2+2) ; if(right==a-1){ return bsr(a,b,x,check,l,(l+r)/2,cur*2+1) ; }else{ return right ; } } int bsl(int a,int b,T x,function check,int l=0,int r=-1,int cur=0){ if(r<0) r = n ; if(!check(num[cur],x)||r<=a||b<=l) return b ; if(cur>=n-1) return cur-(n-1) ; int left = bsl(a,b,x,check,l,(l+r)/2,cur*2+1) ; if(left==b){ return bsl(a,b,x,check,(l+r)/2,r,cur*2+2) ; }else{ return left ; } } } ; int main(){ int n ; cin >> n ; V a(n) ; map pos ; for(int i=0;i> a[i] ; pos[a[i]]++ ; } int cnt = 0 ; for(auto &p:pos){ p.second = cnt ; cnt++ ; } // for(auto p:pos){ // cout << p.first << " " << p.second << endl ; // } SGT mx(pos.size(),[](ll a,ll b){return a+b ;},0) ; SGT mn(pos.size(),[](ll a,ll b){return a+b ;},0) ; SGT s1(pos.size(),[](ll a,ll b){return a+b ;},0) ; SGT s2(pos.size(),[](ll a,ll b){return a+b ;},0) ; V cnt1(n,0),cnt2(n,0),cnt3(n,0),cnt4(n,0) ; for(int i=0;i=0;i--){ cnt2[i] = mn.query(0,pos[a[i]]) ; mn.add(pos[a[i]],1) ; } for(int i=0;i=0;i--){ cnt4[i] = s2.query(0,pos[a[i]]) ; s2.add(pos[a[i]],cnt2[i]) ; } ll ans = 0 ; for(int i=0;i