#include<bits/stdc++.h>
#define rep(i,x,y) for (int i=(x);i<=(y);i++)
#define per(i,x,y) for (int i=(x);i>=(y);i--)
#define ll long long
#define inf 1000000001
using namespace std;
int read(){
	char ch=getchar();int x=0,op=1;
	for (;!isdigit(ch);ch=getchar()) if (ch=='-') op=-1;
	for (;isdigit(ch);ch=getchar()) x=(x<<1)+(x<<3)+ch-'0';
	return x*op;
}
#define N 200005
#define mod 1000000007
#define inv2 500000004
int n,a[N],cnt[N],val0[N],val1[N],val2[N],val3[N],pos[N],ans;
int ksm(int x,int p){
	int ret=1;
	for (;p;p>>=1,x=(ll)x*x%mod) if (p&1) ret=(ll)ret*x%mod;
	return ret;
}
int getinv(int x){return ksm(x,mod-2);}
void upd(int &x,int y){x+=y;x-=x>=mod?mod:0;}
struct bit{
	int t[N];
	void clr(){memset(t,0,sizeof(t));}
	void add(int x,int val){
		for (int i=x;i<=n;i+=i&-i) upd(t[i],val);
	}
	int qry(int x){
		int ret=0;
		for (int i=x;i;i-=i&-i) upd(ret,t[i]);
		return ret;
	}
}tr;
int main(){
	n=read();
	rep (i,1,n) cnt[a[i]=read()]++;
	per (i,n-1,1) cnt[i]+=cnt[i+1]-1;
	rep (i,1,n) if (cnt[i]<=0){puts("0");exit(0);}
	val0[0]=val1[0]=1;
	rep (i,1,n){
		val0[i]=(ll)val0[i-1]*cnt[i]%mod;
		val1[i]=(ll)val1[i-1]*max(cnt[i]-1,1)%mod;
		val2[i]=(ll)val0[i]*getinv(val1[i])%mod;
		val3[i]=(ll)val1[i]*getinv(val0[i])%mod;
	}
	tr.clr();
	pos[n]=n;
	per (i,n-1,1) pos[i]=cnt[i+1]==1?i:pos[i+1];
	per (i,n,1){
		int val=(tr.qry(pos[a[i]])+mod-tr.qry(a[i]-1))%mod;
		upd(ans,(ll)val*val2[a[i]]%mod);
		tr.add(a[i],val3[a[i]]);
	}
	tr.clr();
	pos[1]=1;
	rep (i,2,n) pos[i]=cnt[i]==1?i:pos[i-1];
	per (i,n,1){
		int val=(tr.qry(a[i]-1)+mod-tr.qry(pos[a[i]]-1))%mod;
		upd(ans,(mod-(ll)val*val3[a[i]]%mod)%mod);
		tr.add(a[i],val2[a[i]]);
	}
	ans=(ll)ans*inv2%mod;
	tr.clr();
	per (i,n,1){
		upd(ans,tr.qry(a[i]-1));
		tr.add(a[i],1);
	}
	ans=(ll)ans*val0[n]%mod;
	cout<<ans<<'\n';
	return 0;
}
### Prompt

Create a solution in Cpp for 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 rep(i,x,y) for (int i=(x);i<=(y);i++)
#define per(i,x,y) for (int i=(x);i>=(y);i--)
#define ll long long
#define inf 1000000001
using namespace std;
int read(){
	char ch=getchar();int x=0,op=1;
	for (;!isdigit(ch);ch=getchar()) if (ch=='-') op=-1;
	for (;isdigit(ch);ch=getchar()) x=(x<<1)+(x<<3)+ch-'0';
	return x*op;
}
#define N 200005
#define mod 1000000007
#define inv2 500000004
int n,a[N],cnt[N],val0[N],val1[N],val2[N],val3[N],pos[N],ans;
int ksm(int x,int p){
	int ret=1;
	for (;p;p>>=1,x=(ll)x*x%mod) if (p&1) ret=(ll)ret*x%mod;
	return ret;
}
int getinv(int x){return ksm(x,mod-2);}
void upd(int &x,int y){x+=y;x-=x>=mod?mod:0;}
struct bit{
	int t[N];
	void clr(){memset(t,0,sizeof(t));}
	void add(int x,int val){
		for (int i=x;i<=n;i+=i&-i) upd(t[i],val);
	}
	int qry(int x){
		int ret=0;
		for (int i=x;i;i-=i&-i) upd(ret,t[i]);
		return ret;
	}
}tr;
int main(){
	n=read();
	rep (i,1,n) cnt[a[i]=read()]++;
	per (i,n-1,1) cnt[i]+=cnt[i+1]-1;
	rep (i,1,n) if (cnt[i]<=0){puts("0");exit(0);}
	val0[0]=val1[0]=1;
	rep (i,1,n){
		val0[i]=(ll)val0[i-1]*cnt[i]%mod;
		val1[i]=(ll)val1[i-1]*max(cnt[i]-1,1)%mod;
		val2[i]=(ll)val0[i]*getinv(val1[i])%mod;
		val3[i]=(ll)val1[i]*getinv(val0[i])%mod;
	}
	tr.clr();
	pos[n]=n;
	per (i,n-1,1) pos[i]=cnt[i+1]==1?i:pos[i+1];
	per (i,n,1){
		int val=(tr.qry(pos[a[i]])+mod-tr.qry(a[i]-1))%mod;
		upd(ans,(ll)val*val2[a[i]]%mod);
		tr.add(a[i],val3[a[i]]);
	}
	tr.clr();
	pos[1]=1;
	rep (i,2,n) pos[i]=cnt[i]==1?i:pos[i-1];
	per (i,n,1){
		int val=(tr.qry(a[i]-1)+mod-tr.qry(pos[a[i]]-1))%mod;
		upd(ans,(mod-(ll)val*val3[a[i]]%mod)%mod);
		tr.add(a[i],val2[a[i]]);
	}
	ans=(ll)ans*inv2%mod;
	tr.clr();
	per (i,n,1){
		upd(ans,tr.qry(a[i]-1));
		tr.add(a[i],1);
	}
	ans=(ll)ans*val0[n]%mod;
	cout<<ans<<'\n';
	return 0;
}
```