Language: C++

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5+3;
const int mod=1e9+7;
int mul(int x,int y){
	return (1ll*x*y)%mod;
}
void add(int &x,int y){
	x+=y;
	if(x>=mod){
		x-=mod;
	}
}
void sub(int &x,int y){
	x-=y;
	if(x<0){
		x+=mod;
	}
}
int sub1(int x,int y){
	x-=y;
	if(x<0){
		x+=mod;
	}
	return x;
}
int binpow(int x,int y){
	int tich=1;
	while(y){
		if(y&1){
			tich=mul(tich,x);
		}
		x=mul(x,x);
		y>>=1;
	}
	return tich;
}
int ar[N],cnt[N],pref[N],bit[N],bit1[N],lst[N],nxt[N],bit2[N];
void upd2(int idx,int val){
	while(idx<N){
		add(bit2[idx],val);
		idx+=(idx&-idx);
	}	
}
int getsum2(int idx){
	int sum=0;
	while(idx){
		add(sum,bit2[idx]);
		idx-=(idx&-idx);
	}
	return sum;
}
int get2(int l,int r){
	return sub1(getsum2(r),getsum2(l-1));
}
void upd1(int idx,int val){
	while(idx<N){
		add(bit1[idx],val);
		idx+=(idx&-idx);
	}	
}
int getsum1(int idx){
	int sum=0;
	while(idx){
		add(sum,bit1[idx]);
		idx-=(idx&-idx);
	}
	return sum;
}
int get1(int l,int r){
	return sub1(getsum1(r),getsum1(l-1));
}
void upd(int idx,int val){
	while(idx<N){
		add(bit[idx],val);
		idx+=(idx&-idx);
	}	
}
int getsum(int idx){
	int sum=0;
	while(idx){
		add(sum,bit[idx]);
		idx-=(idx&-idx);
	}
	return sum;
}
int get(int l,int r){
	return sub1(getsum(r),getsum(l-1));
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n,i,j,k,l,tich=1,ans=0;
	cin>>n;
	for(i=1;i<=n;i++){
		cin>>ar[i];
		cnt[ar[i]]++;
	}
	for(i=n;i>0;i--){
		cnt[i]+=cnt[i+1];
	}
	for(i=1;i<=n;i++){
		cnt[i]-=n-i;
		tich=mul(tich,cnt[i]);
		if(cnt[i]<=0){
			cout<<0;
			return 0;
		}
		j=mul(cnt[i]-1,binpow(cnt[i],mod-2));
		if(pref[i-1]){
			pref[i]=mul(pref[i-1],j);
		}
		else{
			pref[i]=j;
		}
		lst[i]=lst[i-1];
		if(!pref[i]){
			lst[i]=i;
		}
	}
	nxt[n]=n+1;
	for(i=n-1;i>0;i--){
		nxt[i]=nxt[i+1];
		if(!pref[i+1]){
			nxt[i]=i+1;
		}
	}
	for(i=1;i<=n;i++){
		add(ans,mul(mul(mul(tich,get(lst[ar[i]],ar[i])),pref[ar[i]]),500000004));	//a(j)<=a(i), (a(j),a(i)]
		add(ans,mul(tich,get1(ar[i]+1,n)));			//a(j)>a(i) (a(i),a(j)]
		if(pref[ar[i]]){
			sub(ans,mul(mul(mul(tich,binpow(pref[ar[i]],mod-2)),get2(ar[i]+1,nxt[i]-1)),500000004));
		}
		else{
			sub(ans,mul(mul(tich,get2(ar[i]+1,nxt[i]-1)),500000004));
		}
		upd1(ar[i],1);
		if(pref[ar[i]]){
			upd(ar[i],binpow(pref[ar[i]],mod-2));
		}
		else{
			upd(ar[i],1);
		}
		upd2(ar[i],pref[ar[i]]);
	}
	cout<<ans;
}#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5+3;
const int mod=1e9+7;
int mul(int x,int y){
	return (1ll*x*y)%mod;
}
void add(int &x,int y){
	x+=y;
	if(x>=mod){
		x-=mod;
	}
}
void sub(int &x,int y){
	x-=y;
	if(x<0){
		x+=mod;
	}
}
int sub1(int x,int y){
	x-=y;
	if(x<0){
		x+=mod;
	}
	return x;
}
int binpow(int x,int y){
	int tich=1;
	while(y){
		if(y&1){
			tich=mul(tich,x);
		}
		x=mul(x,x);
		y>>=1;
	}
	return tich;
}
int ar[N],cnt[N],pref[N],bit[N],bit1[N],lst[N],nxt[N],bit2[N];
void upd2(int idx,int val){
	while(idx<N){
		add(bit2[idx],val);
		idx+=(idx&-idx);
	}	
}
int getsum2(int idx){
	int sum=0;
	while(idx){
		add(sum,bit2[idx]);
		idx-=(idx&-idx);
	}
	return sum;
}
int get2(int l,int r){
	return sub1(getsum2(r),getsum2(l-1));
}
void upd1(int idx,int val){
	while(idx<N){
		add(bit1[idx],val);
		idx+=(idx&-idx);
	}	
}
int getsum1(int idx){
	int sum=0;
	while(idx){
		add(sum,bit1[idx]);
		idx-=(idx&-idx);
	}
	return sum;
}
int get1(int l,int r){
	return sub1(getsum1(r),getsum1(l-1));
}
void upd(int idx,int val){
	while(idx<N){
		add(bit[idx],val);
		idx+=(idx&-idx);
	}	
}
int getsum(int idx){
	int sum=0;
	while(idx){
		add(sum,bit[idx]);
		idx-=(idx&-idx);
	}
	return sum;
}
int get(int l,int r){
	return sub1(getsum(r),getsum(l-1));
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n,i,j,k,l,tich=1,ans=0;
	cin>>n;
	for(i=1;i<=n;i++){
		cin>>ar[i];
		cnt[ar[i]]++;
	}
	for(i=n;i>0;i--){
		cnt[i]+=cnt[i+1];
	}
	for(i=1;i<=n;i++){
		cnt[i]-=n-i;
		tich=mul(tich,cnt[i]);
		if(cnt[i]<=0){
			cout<<0;
			return 0;
		}
		j=mul(cnt[i]-1,binpow(cnt[i],mod-2));
		if(pref[i-1]){
			pref[i]=mul(pref[i-1],j);
		}
		else{
			pref[i]=j;
		}
		lst[i]=lst[i-1];
		if(!pref[i]){
			lst[i]=i;
		}
	}
	nxt[n]=n+1;
	for(i=n-1;i>0;i--){
		nxt[i]=nxt[i+1];
		if(!pref[i+1]){
			nxt[i]=i+1;
		}
	}
	for(i=1;i<=n;i++){
		add(ans,mul(mul(mul(tich,get(lst[ar[i]],ar[i])),pref[ar[i]]),500000004));	//a(j)<=a(i), (a(j),a(i)]
		add(ans,mul(tich,get1(ar[i]+1,n)));			//a(j)>a(i) (a(i),a(j)]
		if(pref[ar[i]]){
			sub(ans,mul(mul(mul(tich,binpow(pref[ar[i]],mod-2)),get2(ar[i]+1,nxt[i]-1)),500000004));
		}
		else{
			sub(ans,mul(mul(tich,get2(ar[i]+1,nxt[i]-1)),500000004));
		}
		upd1(ar[i],1);
		if(pref[ar[i]]){
			upd(ar[i],binpow(pref[ar[i]],mod-2));
		}
		else{
			upd(ar[i],1);
		}
		upd2(ar[i],pref[ar[i]]);
	}
	cout<<ans;
}

Compiler output:
a.cc:148:2: error: stray '#' in program
  148 | }#include<bits/stdc++.h>
      |  ^
a.cc:148:3: error: 'include' does not name a type
  148 | }#include<bits/stdc++.h>
      |   ^~~~~~~
a.cc:151:11: error: redefinition of 'const long long int N'
  151 | const int N=2e5+3;
      |           ^
a.cc:4:11: note: 'const long long int N' previously defined here
    4 | const int N=2e5+3;
      |           ^
a.cc:152:11: error: redefinition of 'const long long int mod'
  152 | const int mod=1e9+7;
      |           ^~~
a.cc:5:11: note: 'const long long int mod' previously defined here
    5 | const int mod=1e9+7;
      |           ^~~
a.cc:153:5: error: redefinition of 'long long int mul(long long int, long long int)'
  153 | int mul(int x,int y){
      |     ^~~
a.cc:6:5: note: 'long long int mul(long long int, long long int)' previously defined here
    6 | int mul(int x,int y){
      |     ^~~
a.cc:156:6: error: redefinition of 'void add(long long int&, long long int)'
  156 | void add(int &x,int y){
      |      ^~~
a.cc:9:6: note: 'void add(long long int&, long long int)' previously defined here
    9 | void add(int &x,int y){
      |      ^~~
a.cc:162:6: error: redefinition of 'void sub(long long int&, long long int)'
  162 | void sub(int &x,int y){
      |      ^~~
a.cc:15:6: note: 'void sub(long long int&, long long int)' previously defined here
   15 | void sub(int &x,int y){
      |      ^~~
a.cc:168:5: error: redefinition of 'long long int sub1(long long int, long long int)'
  168 | int sub1(int x,int y){
      |     ^~~~
a.cc:21:5: note: 'long long int sub1(long long int, long long int)' previously defined here
   21 | int sub1(int x,int y){
      |     ^~~~
a.cc:175:5: error: redefinition of 'long long int binpow(long long int, long long int)'
  175 | int binpow(int x,int y){
      |     ^~~~~~
a.cc:28:5: note: 'long long int binpow(long long int, long long int)' previously defined here
   28 | int binpow(int x,int y){
      |     ^~~~~~
a.cc:186:5: error: redefinition of 'long long int ar [200003]'
  186 | int ar[N],cnt[N],pref[N],bit[N],bit1[N],lst[N],nxt[N],bit2[N];
      |     ^~
a.cc:39:5: note: 'long long int ar [200003]' previously declared here
   39 | int ar[N],cnt[N],pref[N],bit[N],bit1[N],lst[N],nxt[N],bit2[N];
      |     ^~
a.cc:186:11: error: redefinition of 'long long int cnt [200003]'
  186 | int ar[N],cnt[N],pref[N],bit[N],bit1[N],lst[N],nxt[N],bit2[N];
      |           ^~~
a.cc:39:11: note: 'long long int cnt [200003]' previously declared here
   39 | int ar[N],cnt[N],pref[N],bit[N],bit1[N],lst[N],nxt[N],bit2[N];
      |           ^~~
a.cc:186:18: error: redefinition of 'long long int pref [200003]'
  186 | int ar[N],cnt[N],pref[N],bit[N],bit1[N],lst[N],nxt[N],bit2[N];
      |                  ^~~~
a.cc:39:18: note: 'long long int pref [200003]' previously declared here
   39 | int ar[N],cnt[N],pref[N],bit[N],bit1[N],lst[N],nxt[N],bit2[N];
      |                  ^~~~
a.cc:186:26: error: redefinition of 'long long int bit [200003]'
  186 | int ar[N],cnt[N],pref[N],bit[N],bit1[N],lst[N],nxt[N],bit2[N];
      |                          ^~~
a.cc:39:26: note: 'long long int bit [200003]' previously declared here
   39 | int ar[N],cnt[N],pref[N],bit[N],bit1[N],lst[N],nxt[N],bit2[N];
      |                          ^~~
a.cc:186:33: error: redefinition of 'long long int bit1 [200003]'
  186 | int ar[N],cnt[N],pref[N],bit[N],bit1[N],lst[N],nxt[N],bit2[N];
      |                                 ^~~~
a.cc:39:33: note: 'long long int bit1 [200003]' previously declared here
   39 | int ar[N],cnt[N],pref[N],bit[N],bit1[N],lst[N],nxt[N],bit2[N];
      |                                 ^~~~
a.cc:186:41: error: redefinition of 'long long int lst [200003]'
  186 | int ar[N],cnt[N],pref[N],bit[N],bit1[N],lst[N],nxt[N],bit2[N];
      |                                         ^~~
a.cc:39:41: note: 'long long int lst [200003]' previously declared here
   39 | int ar[N],cnt[N],pref[N],bit[N],bit1[N],lst[N],nxt[N],bit2[N];
      |                                         ^~~
a.cc:186:48: error: redefinition of 'long long int nxt [200003]'
  186 | int ar[N],cnt[N],pref[N],bit[N],bit1[N],lst[N],nxt[N],bit2[N];
      |                                                ^~~
a.cc:39:48: note: 'long long int nxt [200003]' previously declared here
   39 | int ar[N],cnt[N],pref[N],bit[N],bit1[N],lst[N],nxt[N],bit2[N];
      |                                                ^~~
a.cc:186:55: error: redefinition of 'long long int bit2 [200003]'
  186 | int ar[N],cnt[N],pref[N],bit[N],bit1[N],lst[N],nxt[N],bit2[N];
      |                                                       ^~~~
a.cc:39:55: note: 'long long int bit2 [200003]' previously declared here
   39 | int ar[N],cnt[N],pref[N],bit[N],bit1[N],lst[N],nxt[N],bit2[N];
      |                                                       ^~~~
a.cc:187:6: error: redefinition of 'void upd2(long long int, long long int)'
  187 | void upd2(int idx,int val){
      |      ^~~~
a.cc:40:6: note: 'void upd2(long long int, long long int)' previously defined here
   40 | void upd2(int idx,int val){
      |      ^~~~
a.cc:193:5: error: redefinition of 'long long int getsum2(long long int)'
  193 | int getsum2(int idx){
      |     ^~~~~~~
a.cc:46:5: note: 'long long int getsum2(long long int)' previously defined here
   46 | int getsum2(int idx){
      |     ^~~~~~~
a.cc:201:5: error: redefinition of 'long long int get2(long long int, long long int)'
  201 | int get2(int l,int r){
      |     ^~~~
a.cc:54:5: note: 'long long int get2(long long int, long long int)' previously defined here
   54 | int get2(int l,int r){
      |     ^~~~
a.cc:204:6: error: redefinition of 'void upd1(long long int, long long int)'
  204 | void upd1(int idx,int val){
      |      ^~~~
a.cc:57:6: note: 'void upd1(long long int, long long int)' previously defined here
   57 | void upd1(int idx,int val){
      |      ^~~~
a.cc:210:5: error: redefinition of 'long long int getsum1(long long int)'
  210 | int getsum1(int idx){
      |     ^~~~~~~
a.cc:63:5: note: 'long long int getsum1(long long int)' previously defined here
   63 | int getsum1(int idx){
      |     ^~~~~~~
a.cc:218:5: error: redefinition of 'long long int get1(long long int, long long int)'
  218 | int get1(int l,int r){
      |     ^~~~
a.cc:71:5: note: 'long long int get1(long long int, long long int)' previously defined here
   71 | int get1(int l,int r){
      |     ^~~~
a.cc:221:6: error: redefinition of 'void upd(long long int, long long int)'
  221 | void upd(int idx,int val){
      |      ^~~
a.cc:74:6: note: 'void upd(long long int, long long int)' previously defined here
   74 | void upd(int idx,int val){
      |      ^~~
a.cc:227:5: error: redefinition of 'long long int getsum(long long int)'
  227 | int getsum(int idx){
      |     ^~~~~~
a.cc:80:5: note: 'long long int getsum(long long int)' previously defined here
   80 | int getsum(int idx){
      |     ^~~~~~
a.cc:235:5: error: redefinition of 'long long int get(long long int, long long int)'
  235 | int get(int l,int r){
      |     ^~~
a.cc:88:5: note: 'long long int get(long long int, long long int)' previously defined here
   88 | int get(int l,int r){
      |     ^~~
a.cc:238:8: error: redefinition of 'int main()'
  238 | signed main(){
      |        ^~~~
a.cc:91:8: note: 'int main()' previously defined here
   91 | signed main(){
      |        ^~~~