#include <bits/stdc++.h>
#define clr(x) memset(x,0,sizeof x)
#define For(i,a,b) for (int i=(a);i<=(b);i++)
#define Fod(i,b,a) for (int i=(b);i>=(a);i--)
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define fi first
#define se second
#define outval(x) cerr<<#x" = "<<x<<endl
#define outtag(x) cerr<<"-----------------"#x"-----------------\n"
#define outarr(a,L,R) cerr<<#a"["<<L<<".."<<R<<"] = ";\
                    For(_x,L,R) cerr<<a[_x]<<" ";cerr<<endl;
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair <int,int> pii;
LL read(){
    LL x=0,f=0;
    char ch=getchar();
    while (!isdigit(ch))
        f=ch=='-',ch=getchar();
    while (isdigit(ch))
        x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    return f?-x:x;
}
const int N=200005,mod=1e9+7;
int inv2=(mod+1)/2;
int Pow(int x,int y){
	int ans=1;
	for (;y;y>>=1,x=(LL)x*x%mod)
		if (y&1)
			ans=(LL)ans*x%mod;
	return ans;
}
void Add(int &x,int y){
	if ((x+=y)>=mod)
		x-=mod;
}
void Del(int &x,int y){
	if ((x-=y)<0)
		x+=mod;
}
int Add(int x){
	return x>=mod?x-mod:x;
}
int Del(int x){
	return x<0?x+mod:x;
}
int n,S=1;
int a[N],b[N],p[N];
namespace bit{
	int c[N];
	void add(int x,int d){
		for (;x<=n;x+=x&-x)
			Add(c[x],d);
	}
	int Ask(int x){
		int ans=0;
		for (;x;x-=x&-x)
			Add(ans,c[x]);
		return ans;
	}
}
bool cmp1(int x,int y){
	return a[x]<a[y];
}
bool cmp2(int x,int y){
	return x<y;
}
int pre[N];
int main(){
	n=read();
	For(i,1,n){
		a[i]=read();
		b[a[i]]++;
	}
	Fod(i,n,1)
		b[i]+=b[i+1];
	For(i,1,n)
		b[i]-=n-i;
	For(i,1,n){
		if (b[i]<=0)
			return puts("0"),0;
		S=(LL)S*b[i]%mod;
	}
	For(i,1,n)
		p[i]=i;
	sort(p+1,p+n+1,cmp1);
	int ans=0;
	for (int i=1,j;i<=n;i=j){
		for (j=i+1;j<=n;j++){
			int flag=0;
			For(k,a[p[j-1]]+1,a[p[j]])
				if (b[k]==1){
					flag=1;
					break;
				}
			if (flag)
				break;
		}
		int mi=a[p[i]],mx=a[p[j-1]];
		pre[mi]=1;
		For(k,mi+1,mx)
			pre[k]=(LL)pre[k-1]*(b[k]-1)%mod*Pow(b[k],mod-2)%mod;
		sort(p+i,p+j,cmp2);
		For(k,i,j-1){
			int x=p[k];
			if (k>i){
				int t=(LL)bit::Ask(a[x])*pre[a[x]]%mod;
				Add(ans,(LL)t*S%mod*inv2%mod);
			}
			bit::add(a[x],Pow(pre[a[x]],mod-2));
		}
		For(k,i,j-1)
			bit::add(a[p[k]],Del(-Pow(pre[a[p[k]]],mod-2)));
		For(k,i,j-1){
			int x=p[k];
			if (k>i){
				int t=Del(bit::Ask(n)-bit::Ask(a[x]));
				t=(LL)t*Pow(pre[a[x]],mod-2)%mod;
				Del(ans,(LL)t*S%mod*inv2%mod);
			}
			bit::add(a[x],pre[a[x]]);
		}
		For(k,i,j-1)
			bit::add(a[p[k]],Del(-pre[a[p[k]]]));
	}
	For(i,1,n){
		int t=bit::Ask(n)-bit::Ask(a[i]);
		bit::add(a[i],1);
		Add(ans,(LL)S*t%mod);
	}
	cout<<ans<<endl;
	return 0;
}
### Prompt

Your task is to create 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 clr(x) memset(x,0,sizeof x)
#define For(i,a,b) for (int i=(a);i<=(b);i++)
#define Fod(i,b,a) for (int i=(b);i>=(a);i--)
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define fi first
#define se second
#define outval(x) cerr<<#x" = "<<x<<endl
#define outtag(x) cerr<<"-----------------"#x"-----------------\n"
#define outarr(a,L,R) cerr<<#a"["<<L<<".."<<R<<"] = ";\
                    For(_x,L,R) cerr<<a[_x]<<" ";cerr<<endl;
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair <int,int> pii;
LL read(){
    LL x=0,f=0;
    char ch=getchar();
    while (!isdigit(ch))
        f=ch=='-',ch=getchar();
    while (isdigit(ch))
        x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    return f?-x:x;
}
const int N=200005,mod=1e9+7;
int inv2=(mod+1)/2;
int Pow(int x,int y){
	int ans=1;
	for (;y;y>>=1,x=(LL)x*x%mod)
		if (y&1)
			ans=(LL)ans*x%mod;
	return ans;
}
void Add(int &x,int y){
	if ((x+=y)>=mod)
		x-=mod;
}
void Del(int &x,int y){
	if ((x-=y)<0)
		x+=mod;
}
int Add(int x){
	return x>=mod?x-mod:x;
}
int Del(int x){
	return x<0?x+mod:x;
}
int n,S=1;
int a[N],b[N],p[N];
namespace bit{
	int c[N];
	void add(int x,int d){
		for (;x<=n;x+=x&-x)
			Add(c[x],d);
	}
	int Ask(int x){
		int ans=0;
		for (;x;x-=x&-x)
			Add(ans,c[x]);
		return ans;
	}
}
bool cmp1(int x,int y){
	return a[x]<a[y];
}
bool cmp2(int x,int y){
	return x<y;
}
int pre[N];
int main(){
	n=read();
	For(i,1,n){
		a[i]=read();
		b[a[i]]++;
	}
	Fod(i,n,1)
		b[i]+=b[i+1];
	For(i,1,n)
		b[i]-=n-i;
	For(i,1,n){
		if (b[i]<=0)
			return puts("0"),0;
		S=(LL)S*b[i]%mod;
	}
	For(i,1,n)
		p[i]=i;
	sort(p+1,p+n+1,cmp1);
	int ans=0;
	for (int i=1,j;i<=n;i=j){
		for (j=i+1;j<=n;j++){
			int flag=0;
			For(k,a[p[j-1]]+1,a[p[j]])
				if (b[k]==1){
					flag=1;
					break;
				}
			if (flag)
				break;
		}
		int mi=a[p[i]],mx=a[p[j-1]];
		pre[mi]=1;
		For(k,mi+1,mx)
			pre[k]=(LL)pre[k-1]*(b[k]-1)%mod*Pow(b[k],mod-2)%mod;
		sort(p+i,p+j,cmp2);
		For(k,i,j-1){
			int x=p[k];
			if (k>i){
				int t=(LL)bit::Ask(a[x])*pre[a[x]]%mod;
				Add(ans,(LL)t*S%mod*inv2%mod);
			}
			bit::add(a[x],Pow(pre[a[x]],mod-2));
		}
		For(k,i,j-1)
			bit::add(a[p[k]],Del(-Pow(pre[a[p[k]]],mod-2)));
		For(k,i,j-1){
			int x=p[k];
			if (k>i){
				int t=Del(bit::Ask(n)-bit::Ask(a[x]));
				t=(LL)t*Pow(pre[a[x]],mod-2)%mod;
				Del(ans,(LL)t*S%mod*inv2%mod);
			}
			bit::add(a[x],pre[a[x]]);
		}
		For(k,i,j-1)
			bit::add(a[p[k]],Del(-pre[a[p[k]]]));
	}
	For(i,1,n){
		int t=bit::Ask(n)-bit::Ask(a[i]);
		bit::add(a[i],1);
		Add(ans,(LL)S*t%mod);
	}
	cout<<ans<<endl;
	return 0;
}
```