#include<bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define uint unsigned
#define pii pair<int,int>
#define pll pair<ll,ll>
#define PB push_back
#define fi first
#define se second
#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++)
#define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--)
#define CLR(a,v) memset(a,v,sizeof(a));
#define CPY(a,b) memcpy(a,b,sizeof(a));
using namespace std;
const int N=200005;
const int mo=1000000007;
int cnt[N],a[N];
int val1[N],val2[N];
int val3[N],val4[N];
int vpr[N],vsu[N];
int pre[N],suf[N];
int ifac[N],iinv[N];
int n,ans,V,S[N];
int power(int x,int y){
	int s=1;
	for (;y;y/=2,x=1ll*x*x%mo)
		if (y&1) s=1ll*s*x%mo;
	return s;
}
int func(int l,int r){
	if (pre[r-1]>=l+1) return 0;
	return 1ll*vpr[l-1]*vsu[r+1]%mo*val4[l]%mo*val3[r]%mo*ifac[r-1]%mo*iinv[l]%mo;
}
struct TARR{
	int t[N];
	void change(int x,int v){
		for (;x<=n;x+=x&(-x)) t[x]=(t[x]+v)%mo;
	}
	int ask(int x){
		ll ans=0;
		for (;x;x-=x&(-x)) ans+=t[x];
		return ans%mo;
	}
	int ask(int l,int r){
		l=max(l,1); r=min(r,n);
		if (l>r) return 0;
		return (ask(r)+mo-ask(l-1))%mo;
	}
}T1,T2,T3;
void update(int x,int y){
	if (a[x]>a[y]) ans=(ans+mo-func(a[y],a[x]))%mo;
	if (a[x]<a[y]) ans=(ans+func(a[x],a[y]))%mo;
}
int main(){
	scanf("%d",&n);
	For(i,1,n) scanf("%d",&a[i]),cnt[a[i]]++;
	For(i,1,n) S[i]=S[i-1]+cnt[i];
	V=1;
	For(i,1,n){
		val1[i]=val2[i]=val3[i]=1;
		For(j,S[i-1]+1,S[i]){
			val1[i]=1ll*val1[i]*(i-j+1)%mo;
			val2[i]=1ll*val2[i]*(i-j)%mo;
			if (j!=S[i-1]+1)
				val3[i]=1ll*val3[i]*(i-j+1)%mo;
		}
		val4[i]=1ll*val1[i]*(i-S[i])%mo;
		V=1ll*V*val1[i]%mo;
	}
	if (!V) return puts("0"),0;
	vpr[0]=vsu[n+1]=1;
	For(i,1,n) vpr[i]=1ll*vpr[i-1]*val1[i]%mo;
	Rep(i,n,1) vsu[i]=1ll*vsu[i+1]*val1[i]%mo;
	pre[0]=0; suf[n+1]=n+1;
	For(i,1,n) pre[i]=(val2[i]==0?i:pre[i-1]);
	Rep(i,n,1) suf[i]=(val2[i]==0?i:suf[i+1]);
	For(i,1,n) if (!val2[i]) val2[i]=1;
	ifac[0]=1;
	For(i,1,n) ifac[i]=1ll*ifac[i-1]*val2[i]%mo;
	iinv[n]=power(ifac[n],mo-2);
	Rep(i,n,1) iinv[i-1]=1ll*iinv[i]*val2[i]%mo;
	For(i,1,n){
		int vl=1ll*vpr[a[i]-1]*val4[a[i]]%mo*iinv[a[i]]%mo;
		int vr=1ll*vsu[a[i]+1]*val3[a[i]]%mo*ifac[a[i]-1]%mo;
		ans=(ans+1ll*(i-1-T1.ask(a[i]-1))*V)%mo;
		ans=(ans+1ll*(i-1-T1.ask(a[i]))*V)%mo;
		ans=(ans+mo-1ll*vl*T2.ask(a[i]+1,suf[a[i]+1])%mo)%mo;
		ans=(ans+1ll*vr*T3.ask(pre[a[i]-1],a[i]-1)%mo)%mo;
		//For(j,1,i-1) update(j,i);
		T1.change(a[i],1);
		T2.change(a[i],vr);
		T3.change(a[i],vl);
	}
	ans=1ll*ans*(mo+1)/2%mo;
	printf("%d\n",ans);
}
//return 1ll*vpr[l-1]*vsu[r+1]%mo*val4[l]%mo*val3[r]%mo*ifac[r-1]%mo*iinv[l]%mo;
### Prompt

Your challenge is to write a Cpp solution to the following problem:

Snuke has an integer sequence A whose length is N. He likes permutations of (1, 2, ..., N), P, that satisfy the following condition:

* P_i \leq A_i for all i ( 1 \leq i \leq N ).



Snuke is interested in the inversion numbers of such permutations. Find the sum of the inversion numbers over all permutations that satisfy the condition. Since this can be extremely large, compute the sum modulo 10^9+7.

Constraints

* 1 \leq N \leq 2 \times 10^5
* 1 \leq A_i \leq N ( 1 \leq i \leq N )
* All values in input are integers.

Input

Input is given from Standard Input in the following format:


N
A_1 A_2 ... A_N


Output

Print the sum of the inversion numbers over all permutations that satisfy the condition.

Examples

Input

3
2 3 3


Output

4


Input

6
4 2 5 1 6 3


Output

7


Input

5
4 4 4 4 4


Output

0


Input

30
22 30 15 20 10 29 11 29 28 11 26 10 18 28 22 5 29 16 24 24 27 10 21 30 29 19 28 27 18 23


Output

848414012

### Response

```cpp
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define uint unsigned
#define pii pair<int,int>
#define pll pair<ll,ll>
#define PB push_back
#define fi first
#define se second
#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++)
#define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--)
#define CLR(a,v) memset(a,v,sizeof(a));
#define CPY(a,b) memcpy(a,b,sizeof(a));
using namespace std;
const int N=200005;
const int mo=1000000007;
int cnt[N],a[N];
int val1[N],val2[N];
int val3[N],val4[N];
int vpr[N],vsu[N];
int pre[N],suf[N];
int ifac[N],iinv[N];
int n,ans,V,S[N];
int power(int x,int y){
	int s=1;
	for (;y;y/=2,x=1ll*x*x%mo)
		if (y&1) s=1ll*s*x%mo;
	return s;
}
int func(int l,int r){
	if (pre[r-1]>=l+1) return 0;
	return 1ll*vpr[l-1]*vsu[r+1]%mo*val4[l]%mo*val3[r]%mo*ifac[r-1]%mo*iinv[l]%mo;
}
struct TARR{
	int t[N];
	void change(int x,int v){
		for (;x<=n;x+=x&(-x)) t[x]=(t[x]+v)%mo;
	}
	int ask(int x){
		ll ans=0;
		for (;x;x-=x&(-x)) ans+=t[x];
		return ans%mo;
	}
	int ask(int l,int r){
		l=max(l,1); r=min(r,n);
		if (l>r) return 0;
		return (ask(r)+mo-ask(l-1))%mo;
	}
}T1,T2,T3;
void update(int x,int y){
	if (a[x]>a[y]) ans=(ans+mo-func(a[y],a[x]))%mo;
	if (a[x]<a[y]) ans=(ans+func(a[x],a[y]))%mo;
}
int main(){
	scanf("%d",&n);
	For(i,1,n) scanf("%d",&a[i]),cnt[a[i]]++;
	For(i,1,n) S[i]=S[i-1]+cnt[i];
	V=1;
	For(i,1,n){
		val1[i]=val2[i]=val3[i]=1;
		For(j,S[i-1]+1,S[i]){
			val1[i]=1ll*val1[i]*(i-j+1)%mo;
			val2[i]=1ll*val2[i]*(i-j)%mo;
			if (j!=S[i-1]+1)
				val3[i]=1ll*val3[i]*(i-j+1)%mo;
		}
		val4[i]=1ll*val1[i]*(i-S[i])%mo;
		V=1ll*V*val1[i]%mo;
	}
	if (!V) return puts("0"),0;
	vpr[0]=vsu[n+1]=1;
	For(i,1,n) vpr[i]=1ll*vpr[i-1]*val1[i]%mo;
	Rep(i,n,1) vsu[i]=1ll*vsu[i+1]*val1[i]%mo;
	pre[0]=0; suf[n+1]=n+1;
	For(i,1,n) pre[i]=(val2[i]==0?i:pre[i-1]);
	Rep(i,n,1) suf[i]=(val2[i]==0?i:suf[i+1]);
	For(i,1,n) if (!val2[i]) val2[i]=1;
	ifac[0]=1;
	For(i,1,n) ifac[i]=1ll*ifac[i-1]*val2[i]%mo;
	iinv[n]=power(ifac[n],mo-2);
	Rep(i,n,1) iinv[i-1]=1ll*iinv[i]*val2[i]%mo;
	For(i,1,n){
		int vl=1ll*vpr[a[i]-1]*val4[a[i]]%mo*iinv[a[i]]%mo;
		int vr=1ll*vsu[a[i]+1]*val3[a[i]]%mo*ifac[a[i]-1]%mo;
		ans=(ans+1ll*(i-1-T1.ask(a[i]-1))*V)%mo;
		ans=(ans+1ll*(i-1-T1.ask(a[i]))*V)%mo;
		ans=(ans+mo-1ll*vl*T2.ask(a[i]+1,suf[a[i]+1])%mo)%mo;
		ans=(ans+1ll*vr*T3.ask(pre[a[i]-1],a[i]-1)%mo)%mo;
		//For(j,1,i-1) update(j,i);
		T1.change(a[i],1);
		T2.change(a[i],vr);
		T3.change(a[i],vl);
	}
	ans=1ll*ans*(mo+1)/2%mo;
	printf("%d\n",ans);
}
//return 1ll*vpr[l-1]*vsu[r+1]%mo*val4[l]%mo*val3[r]%mo*ifac[r-1]%mo*iinv[l]%mo;
```