#include #include #include using namespace atcoder; using mint = modint1000000007; using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i) #define Inf 1000000000 int main(){ int n; cin>>n; vector a(n); rep(i,n)scanf("%d",&a[i]); auto t = a; sort(t.begin(),t.end()); t.erase(unique(t.begin(),t.end()),t.end()); rep(i,n){ a[i] = distance(t.begin(),lower_bound(t.begin(),t.end(),a[i])); } vector Rm(n),RM(n); { fenwick_tree F(t.size()); for(int i=n-1;i>=0;i--){ Rm[i] = F.sum(0,a[i]); RM[i] = F.sum(a[i]+1,t.size()); F.add(a[i],1LL); } } long long ans = 0LL; { fenwick_tree F(t.size()); rep(i,n){ ans += Rm[i] * F.sum(0,a[i]); ans += RM[i] * F.sum(a[i]+1,t.size()); F.add(a[i],1LL); } } vector> dp(t.size()); rep(i,n){ dp[a[i]].push_back(i); } rep(i,t.size()){ long long cur = 0LL; for(int j=1;j