#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<deque>
#include<stack>
#include<string>
#include<string.h>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#include<stdlib.h>
#include<cassert>
#include<time.h>
#include<bitset>
using namespace std;
const long long mod=1000000007;
const long long inf=mod*mod;
const long long d2=(mod+1)/2;
const long double EPS=1e-13;
const long double PI=acos(-1.0);
int ABS(int a){return max(a,-a);}
long long ABS(long long a){return max(a,-a);}
long double ABS(long double a){return max(a,-a);}
int A[210000];
long long bit[210000];
long long sum(int a,int b){
	if(a>b)return 0;
	if(a)return (mod+sum(0,b)-sum(0,a-1))%mod;
	long long ret=0;
	for(;b>=0;b=(b&(b+1))-1)ret=(ret+bit[b])%mod;
	return ret;
}
void add(int a,long long b){
	for(;a<210000;a|=a+1)bit[a]=(bit[a]+b)%mod;
}
long long pw(long long a,long long b){
	long long ret=1;
	while(b){
		if(b%2)ret=ret*a%mod;
		a=a*a%mod;
		b/=2;
	}
	return ret;
}
long long inv[210000];
int val[210000];
long long V[210000];
long long D[210000];
int x[210000];
int main(){
	inv[1]=1;
	for(int i=2;i<210000;i++){
		inv[i]=(mod-(mod/i)*inv[mod%i]%mod)%mod;
	}
	int n;scanf("%d",&n);
	for(int i=0;i<n;i++)scanf("%d",A+i);
	for(int i=0;i<n;i++){
		val[A[i]]++;
	}
	long long ret=0;
	long long S=1;
	D[0]=1;
	for(int i=n;i;i--){
		val[i-1]+=val[i];
	}
	for(int i=1;i<=n;i++){
	//	val[i+1]+=val[i];
		val[i]-=(n-i);
		if(val[i]<=0){
			printf("0\n");return 0;
		}
		S=S*val[i]%mod;
		V[i]=(val[i]-1)*inv[val[i]]%mod;
		if(V[i]==0){
			D[i]=D[i-1];
			x[i]=x[i-1]+1;
		}else{
			D[i]=D[i-1]*V[i]%mod;
			x[i]=x[i-1];
		}
	}
	//for(int i=1;i<=n;i++){printf("%d\n",val[i]);}

	for(int i=0;i<n;i++){
		int left=lower_bound(x,x+n+1,x[A[i]])-x;
		long long tmp1=sum(left,A[i]);
		add(A[i],pw(D[A[i]],mod-2));
		ret=(ret+tmp1*D[A[i]]%mod*inv[2]%mod*S)%mod;
	}
	for(int i=0;i<210000;i++)bit[i]=0;
	for(int i=0;i<n;i++){
		int right=upper_bound(x,x+n+1,x[A[i]])-x-1;
		long long tmp2=sum(A[i]+1,right);
		add(A[i],D[A[i]]);
		ret=(ret+mod-tmp2*pw(D[A[i]],mod-2)%mod*inv[2]%mod*S%mod)%mod;
	}
	long long pr=0;
	for(int i=0;i<210000;i++)bit[i]=0;
	for(int i=0;i<n;i++){
		pr+=sum(A[i]+1,n+1);
		add(A[i],1);
	}
	pr%=mod;
	ret=(ret+pr*S)%mod;
	printf("%lld\n",ret);
}
### Prompt

Please formulate 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<stdio.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<deque>
#include<stack>
#include<string>
#include<string.h>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#include<stdlib.h>
#include<cassert>
#include<time.h>
#include<bitset>
using namespace std;
const long long mod=1000000007;
const long long inf=mod*mod;
const long long d2=(mod+1)/2;
const long double EPS=1e-13;
const long double PI=acos(-1.0);
int ABS(int a){return max(a,-a);}
long long ABS(long long a){return max(a,-a);}
long double ABS(long double a){return max(a,-a);}
int A[210000];
long long bit[210000];
long long sum(int a,int b){
	if(a>b)return 0;
	if(a)return (mod+sum(0,b)-sum(0,a-1))%mod;
	long long ret=0;
	for(;b>=0;b=(b&(b+1))-1)ret=(ret+bit[b])%mod;
	return ret;
}
void add(int a,long long b){
	for(;a<210000;a|=a+1)bit[a]=(bit[a]+b)%mod;
}
long long pw(long long a,long long b){
	long long ret=1;
	while(b){
		if(b%2)ret=ret*a%mod;
		a=a*a%mod;
		b/=2;
	}
	return ret;
}
long long inv[210000];
int val[210000];
long long V[210000];
long long D[210000];
int x[210000];
int main(){
	inv[1]=1;
	for(int i=2;i<210000;i++){
		inv[i]=(mod-(mod/i)*inv[mod%i]%mod)%mod;
	}
	int n;scanf("%d",&n);
	for(int i=0;i<n;i++)scanf("%d",A+i);
	for(int i=0;i<n;i++){
		val[A[i]]++;
	}
	long long ret=0;
	long long S=1;
	D[0]=1;
	for(int i=n;i;i--){
		val[i-1]+=val[i];
	}
	for(int i=1;i<=n;i++){
	//	val[i+1]+=val[i];
		val[i]-=(n-i);
		if(val[i]<=0){
			printf("0\n");return 0;
		}
		S=S*val[i]%mod;
		V[i]=(val[i]-1)*inv[val[i]]%mod;
		if(V[i]==0){
			D[i]=D[i-1];
			x[i]=x[i-1]+1;
		}else{
			D[i]=D[i-1]*V[i]%mod;
			x[i]=x[i-1];
		}
	}
	//for(int i=1;i<=n;i++){printf("%d\n",val[i]);}

	for(int i=0;i<n;i++){
		int left=lower_bound(x,x+n+1,x[A[i]])-x;
		long long tmp1=sum(left,A[i]);
		add(A[i],pw(D[A[i]],mod-2));
		ret=(ret+tmp1*D[A[i]]%mod*inv[2]%mod*S)%mod;
	}
	for(int i=0;i<210000;i++)bit[i]=0;
	for(int i=0;i<n;i++){
		int right=upper_bound(x,x+n+1,x[A[i]])-x-1;
		long long tmp2=sum(A[i]+1,right);
		add(A[i],D[A[i]]);
		ret=(ret+mod-tmp2*pw(D[A[i]],mod-2)%mod*inv[2]%mod*S%mod)%mod;
	}
	long long pr=0;
	for(int i=0;i<210000;i++)bit[i]=0;
	for(int i=0;i<n;i++){
		pr+=sum(A[i]+1,n+1);
		add(A[i],1);
	}
	pr%=mod;
	ret=(ret+pr*S)%mod;
	printf("%lld\n",ret);
}
```