#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define Rep(i,a,b) for(register int i=(a);i<=int(b);++i)
#define Dep(i,a,b) for(register int i=(a);i>=int(b);--i)
#define rep(i,a,b) for(register int i=(a);i<int(b);++i)
#define mem(x,v) memset(x,v,sizeof(x))
#define gc getchar
#define pc putchar
#define fi first
#define queue QQQ
#define se second
#define debug(x) cout << #x" = " << x << endl;
#define pp(x,y) cout << "pp: " << x << " " << y << endl;
#define rank __RAnK
inline ll read(){
	register ll x=0,f=1;register char c=gc();
	for(;!isdigit(c);c=gc())if(c=='-')f=-1;
	for(;isdigit(c);c=gc())x=(x<<1)+(x<<3)+(c^48);
	return x*f;
}
#define rd read
void write(ll x){if(x<0)x=-x,pc('-');if(x>=10)write(x/10);putchar(x%10+'0');}
void writeln(ll x){write(x);puts("");}
const int maxn = 2e5+233;
const int mod = 1e9+7;
int n;
int c1[maxn],c2[maxn],cnt[maxn],a[maxn],tot,D[maxn],invD[maxn];
int zero[maxn],S[maxn];
#define lowbit(x) ((x) & (-x))
inline int qpow(int a,int b){
	int ans = 1;
	for(;b;b>>=1,a=1ll*a*a%mod)
		if(b&1) ans=1ll*ans*a%mod;
	return ans;
}
void add(int c[],int x,int v){
	for(;x<=n;x+=lowbit(x))
		(c[x] += v) %= mod;
}
int query(int c[],int x){
	int ans = 0;
	for(;x;x-=lowbit(x))
		(ans += c[x]) %= mod;
	return ans;
}
int main(){
	n = rd();
	Rep(i,1,n) cnt[a[i]=rd()]++;
	Dep(i,n-1,1) cnt[i]+=cnt[i+1];
	tot = 1;
	Dep(i,n,1) cnt[i] -= n-i;
	Dep(i,n,1) tot = 1ll * tot * cnt[i] % mod; 
	if(!tot){puts("0");return 0;}
	S[0] = 1;D[0] = 1;
	Rep(i,1,n){
		if(cnt[i]==1) D[i] = D[i-1]; else  
					  D[i] = 1ll * (cnt[i]-1) * qpow(cnt[i],mod-2) % mod * D[i-1] % mod;
		if(cnt[i]==1){
			zero[i] = zero[i-1] + 1;
			S[zero[i]] = i;
		} else{
			zero[i] = zero[i-1];
		}
		invD[i] = qpow(D[i],mod-2);
	}
	mem(c1,0);mem(c2,0);
	ll ans = 0;
	Rep(i,1,n){
		ans = (ans + 1ll * tot * qpow(2,mod-2) %mod * (query(c1,a[i]) - query(c1,a[i]-1))) % mod;//same
		ans = (ans + 1ll * tot * qpow(2,mod-2) %mod * D[a[i]] %mod * (query(c2,a[i]-1) - query(c2,S[zero[a[i]]]-1) + mod) % mod) % mod;//small
		add(c1,a[i],1);
		add(c2,a[i],invD[a[i]]);
	}//相同的
//	writeln(ans);
	mem(c1,0);mem(c2,0);
	Dep(i,n,1){
		ans = (ans - 1ll * tot * qpow(2,mod-2) %mod * D[a[i]] %mod * (query(c2,a[i]-1) - query(c2,S[zero[a[i]]]-1) + mod)% mod + mod) % mod;
		ans = (ans + 1ll * query(c1,a[i]-1) * tot) % mod;
		add(c1,a[i],1);
		add(c2,a[i],invD[a[i]]);
	} 
	writeln(ans);
	return 0;
}
/*
考虑枚举出某个数

需要查询它右边的答案

1、右边和它相同的。 
	倒着做， 
2、右边比它小的。 

3、右边比它大的。 

sum * sum(D[i]) / (sum(D[])) 
*/
### Prompt

Please create a solution in Cpp 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>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define Rep(i,a,b) for(register int i=(a);i<=int(b);++i)
#define Dep(i,a,b) for(register int i=(a);i>=int(b);--i)
#define rep(i,a,b) for(register int i=(a);i<int(b);++i)
#define mem(x,v) memset(x,v,sizeof(x))
#define gc getchar
#define pc putchar
#define fi first
#define queue QQQ
#define se second
#define debug(x) cout << #x" = " << x << endl;
#define pp(x,y) cout << "pp: " << x << " " << y << endl;
#define rank __RAnK
inline ll read(){
	register ll x=0,f=1;register char c=gc();
	for(;!isdigit(c);c=gc())if(c=='-')f=-1;
	for(;isdigit(c);c=gc())x=(x<<1)+(x<<3)+(c^48);
	return x*f;
}
#define rd read
void write(ll x){if(x<0)x=-x,pc('-');if(x>=10)write(x/10);putchar(x%10+'0');}
void writeln(ll x){write(x);puts("");}
const int maxn = 2e5+233;
const int mod = 1e9+7;
int n;
int c1[maxn],c2[maxn],cnt[maxn],a[maxn],tot,D[maxn],invD[maxn];
int zero[maxn],S[maxn];
#define lowbit(x) ((x) & (-x))
inline int qpow(int a,int b){
	int ans = 1;
	for(;b;b>>=1,a=1ll*a*a%mod)
		if(b&1) ans=1ll*ans*a%mod;
	return ans;
}
void add(int c[],int x,int v){
	for(;x<=n;x+=lowbit(x))
		(c[x] += v) %= mod;
}
int query(int c[],int x){
	int ans = 0;
	for(;x;x-=lowbit(x))
		(ans += c[x]) %= mod;
	return ans;
}
int main(){
	n = rd();
	Rep(i,1,n) cnt[a[i]=rd()]++;
	Dep(i,n-1,1) cnt[i]+=cnt[i+1];
	tot = 1;
	Dep(i,n,1) cnt[i] -= n-i;
	Dep(i,n,1) tot = 1ll * tot * cnt[i] % mod; 
	if(!tot){puts("0");return 0;}
	S[0] = 1;D[0] = 1;
	Rep(i,1,n){
		if(cnt[i]==1) D[i] = D[i-1]; else  
					  D[i] = 1ll * (cnt[i]-1) * qpow(cnt[i],mod-2) % mod * D[i-1] % mod;
		if(cnt[i]==1){
			zero[i] = zero[i-1] + 1;
			S[zero[i]] = i;
		} else{
			zero[i] = zero[i-1];
		}
		invD[i] = qpow(D[i],mod-2);
	}
	mem(c1,0);mem(c2,0);
	ll ans = 0;
	Rep(i,1,n){
		ans = (ans + 1ll * tot * qpow(2,mod-2) %mod * (query(c1,a[i]) - query(c1,a[i]-1))) % mod;//same
		ans = (ans + 1ll * tot * qpow(2,mod-2) %mod * D[a[i]] %mod * (query(c2,a[i]-1) - query(c2,S[zero[a[i]]]-1) + mod) % mod) % mod;//small
		add(c1,a[i],1);
		add(c2,a[i],invD[a[i]]);
	}//相同的
//	writeln(ans);
	mem(c1,0);mem(c2,0);
	Dep(i,n,1){
		ans = (ans - 1ll * tot * qpow(2,mod-2) %mod * D[a[i]] %mod * (query(c2,a[i]-1) - query(c2,S[zero[a[i]]]-1) + mod)% mod + mod) % mod;
		ans = (ans + 1ll * query(c1,a[i]-1) * tot) % mod;
		add(c1,a[i],1);
		add(c2,a[i],invD[a[i]]);
	} 
	writeln(ans);
	return 0;
}
/*
考虑枚举出某个数

需要查询它右边的答案

1、右边和它相同的。 
	倒着做， 
2、右边比它小的。 

3、右边比它大的。 

sum * sum(D[i]) / (sum(D[])) 
*/
```