#include<bits/stdc++.h>
using namespace std;
const long long maxn=200100;
const long long md=1e9+7;
#define mid ((l+r)>>1)
long long n;
long long a[maxn];
vector<int> vec[maxn];
long long tr1[maxn<<2],tr2[maxn<<2];
long long cnt[maxn<<2],tg[maxn<<2];
long long f[maxn],mtg[maxn<<2];
void Add_to(long long h,long long v){
	(tg[h]+=v)%=md;
	tr2[h]=(tr2[h]+tr1[h]*v)%md;
}
void Mult(long long h,long long v){
	mtg[h]=mtg[h]*v%md;
	tr1[h]=tr1[h]*v%md;
	tr2[h]=tr2[h]*v%md;
}
void push_down(long long h){
	if(tg[h]){
		Add_to(h<<1,tg[h]);
		Add_to(h<<1|1,tg[h]);
		tg[h]=0;
	}
	if(mtg[h]!=1){
		Mult(h<<1,mtg[h]);
		Mult(h<<1|1,mtg[h]);
		mtg[h]=1;
	}
}
void push_up(long long h){
	cnt[h]=(cnt[h<<1]+cnt[h<<1|1])%md;
	tr1[h]=(tr1[h<<1]+tr1[h<<1|1])%md;
	tr2[h]=(tr2[h<<1]+tr2[h<<1|1])%md;
}
void Add_to(long long h,long long l,long long r,long long s,long long t){
	if(s<=l&&r<=t){
		Add_to(h,1);
		return;
	}
	push_down(h);
	if(t<=mid)
		Add_to(h<<1,l,mid,s,t);
	else if(s>mid)
		Add_to(h<<1|1,mid+1,r,s,t);
	else{
		Add_to(h<<1,l,mid,s,mid);
		Add_to(h<<1|1,mid+1,r,s,t);
	}
	push_up(h);
}
void Insert(long long h,long long l,long long r,long long p,long long s1,long long s2){
	if(l==r){
		tr1[h]=s1,tr2[h]=s2;
		cnt[h]=1;
		return;
	}
	push_down(h);
	if(p<=mid) Insert(h<<1,l,mid,p,s1,s2);
	else Insert(h<<1|1,mid+1,r,p,s1,s2);
	push_up(h);
}
long long Query_cnt(long long h,long long l,long long r,long long s,long long t){
	if(s<=l&&r<=t) return cnt[h];
	push_down(h);
	if(t<=mid) return Query_cnt(h<<1,l,mid,s,t);
	else if(s>mid) return Query_cnt(h<<1|1,mid+1,r,s,t);
	else
		return (Query_cnt(h<<1,l,mid,s,mid)+Query_cnt(h<<1|1,mid+1,r,mid+1,t))%md;
}
long long Query1(long long h,long long l,long long r,long long s,long long t){
	if(s<=l&&r<=t) return tr1[h];
	push_down(h);
	if(t<=mid) return Query1(h<<1,l,mid,s,t);
	else if(s>mid) return Query1(h<<1|1,mid+1,r,s,t);
	else
		return (Query1(h<<1,l,mid,s,mid)+Query1(h<<1|1,mid+1,r,mid+1,t))%md;
}
long long Query2(long long h,long long l,long long r,long long s,long long t){
	if(s<=l&&r<=t) return tr2[h];
	push_down(h);
	if(t<=mid) return Query2(h<<1,l,mid,s,t);
	else if(s>mid) return Query2(h<<1|1,mid+1,r,s,t);
	else
		return (Query2(h<<1,l,mid,s,mid)+Query2(h<<1|1,mid+1,r,mid+1,t))%md;
}
void Build_tree(long long h,long long l,long long r){
	mtg[h]=1;
	if(l==r) return;
	Build_tree(h<<1,l,mid);
	Build_tree(h<<1|1,mid+1,r);
}
int main(){
	scanf("%lld",&n);
	for(long long i=1;i<=n;i++){
		scanf("%lld",&a[i]);
		vec[a[i]].push_back(i);
	}
	Build_tree(1,1,n);
	long long sum0=1,sum2=0;
	long long siz=0;
	for(long long i=n;i>=1;i--){
		sum2=(sum2+vec[i].size()*(long long)(vec[i].size()-1)/2%md*sum0%md)%md;
		sum2=(sum2+vec[i].size()*Query1(1,1,n,1,n))%md;
		for(vector<int>::iterator it=vec[i].begin();it!=vec[i].end();it++)
			Add_to(1,1,n,*it,n);
		for(vector<int>::iterator it=vec[i].begin();it!=vec[i].end();it++)
			Insert(1,1,n,*it,sum0,sum0*Query_cnt(1,1,n,1,*it)%md);
		siz+=vec[i].size();
		f[i]=(Query2(1,1,n,1,n)-sum2+md+f[i+1]*(siz-(n-i)))%md;
		if(siz-(n-i)-2<0) sum2=0;
		else sum2=sum2*(siz-(n-i)-2)%md;
		sum0=sum0*(siz-(n-i))%md;
		Mult(1,siz-(n-i)-1);
	}
	printf("%lld\n",f[1]);
	return 0;
}

### 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<bits/stdc++.h>
using namespace std;
const long long maxn=200100;
const long long md=1e9+7;
#define mid ((l+r)>>1)
long long n;
long long a[maxn];
vector<int> vec[maxn];
long long tr1[maxn<<2],tr2[maxn<<2];
long long cnt[maxn<<2],tg[maxn<<2];
long long f[maxn],mtg[maxn<<2];
void Add_to(long long h,long long v){
	(tg[h]+=v)%=md;
	tr2[h]=(tr2[h]+tr1[h]*v)%md;
}
void Mult(long long h,long long v){
	mtg[h]=mtg[h]*v%md;
	tr1[h]=tr1[h]*v%md;
	tr2[h]=tr2[h]*v%md;
}
void push_down(long long h){
	if(tg[h]){
		Add_to(h<<1,tg[h]);
		Add_to(h<<1|1,tg[h]);
		tg[h]=0;
	}
	if(mtg[h]!=1){
		Mult(h<<1,mtg[h]);
		Mult(h<<1|1,mtg[h]);
		mtg[h]=1;
	}
}
void push_up(long long h){
	cnt[h]=(cnt[h<<1]+cnt[h<<1|1])%md;
	tr1[h]=(tr1[h<<1]+tr1[h<<1|1])%md;
	tr2[h]=(tr2[h<<1]+tr2[h<<1|1])%md;
}
void Add_to(long long h,long long l,long long r,long long s,long long t){
	if(s<=l&&r<=t){
		Add_to(h,1);
		return;
	}
	push_down(h);
	if(t<=mid)
		Add_to(h<<1,l,mid,s,t);
	else if(s>mid)
		Add_to(h<<1|1,mid+1,r,s,t);
	else{
		Add_to(h<<1,l,mid,s,mid);
		Add_to(h<<1|1,mid+1,r,s,t);
	}
	push_up(h);
}
void Insert(long long h,long long l,long long r,long long p,long long s1,long long s2){
	if(l==r){
		tr1[h]=s1,tr2[h]=s2;
		cnt[h]=1;
		return;
	}
	push_down(h);
	if(p<=mid) Insert(h<<1,l,mid,p,s1,s2);
	else Insert(h<<1|1,mid+1,r,p,s1,s2);
	push_up(h);
}
long long Query_cnt(long long h,long long l,long long r,long long s,long long t){
	if(s<=l&&r<=t) return cnt[h];
	push_down(h);
	if(t<=mid) return Query_cnt(h<<1,l,mid,s,t);
	else if(s>mid) return Query_cnt(h<<1|1,mid+1,r,s,t);
	else
		return (Query_cnt(h<<1,l,mid,s,mid)+Query_cnt(h<<1|1,mid+1,r,mid+1,t))%md;
}
long long Query1(long long h,long long l,long long r,long long s,long long t){
	if(s<=l&&r<=t) return tr1[h];
	push_down(h);
	if(t<=mid) return Query1(h<<1,l,mid,s,t);
	else if(s>mid) return Query1(h<<1|1,mid+1,r,s,t);
	else
		return (Query1(h<<1,l,mid,s,mid)+Query1(h<<1|1,mid+1,r,mid+1,t))%md;
}
long long Query2(long long h,long long l,long long r,long long s,long long t){
	if(s<=l&&r<=t) return tr2[h];
	push_down(h);
	if(t<=mid) return Query2(h<<1,l,mid,s,t);
	else if(s>mid) return Query2(h<<1|1,mid+1,r,s,t);
	else
		return (Query2(h<<1,l,mid,s,mid)+Query2(h<<1|1,mid+1,r,mid+1,t))%md;
}
void Build_tree(long long h,long long l,long long r){
	mtg[h]=1;
	if(l==r) return;
	Build_tree(h<<1,l,mid);
	Build_tree(h<<1|1,mid+1,r);
}
int main(){
	scanf("%lld",&n);
	for(long long i=1;i<=n;i++){
		scanf("%lld",&a[i]);
		vec[a[i]].push_back(i);
	}
	Build_tree(1,1,n);
	long long sum0=1,sum2=0;
	long long siz=0;
	for(long long i=n;i>=1;i--){
		sum2=(sum2+vec[i].size()*(long long)(vec[i].size()-1)/2%md*sum0%md)%md;
		sum2=(sum2+vec[i].size()*Query1(1,1,n,1,n))%md;
		for(vector<int>::iterator it=vec[i].begin();it!=vec[i].end();it++)
			Add_to(1,1,n,*it,n);
		for(vector<int>::iterator it=vec[i].begin();it!=vec[i].end();it++)
			Insert(1,1,n,*it,sum0,sum0*Query_cnt(1,1,n,1,*it)%md);
		siz+=vec[i].size();
		f[i]=(Query2(1,1,n,1,n)-sum2+md+f[i+1]*(siz-(n-i)))%md;
		if(siz-(n-i)-2<0) sum2=0;
		else sum2=sum2*(siz-(n-i)-2)%md;
		sum0=sum0*(siz-(n-i))%md;
		Mult(1,siz-(n-i)-1);
	}
	printf("%lld\n",f[1]);
	return 0;
}

```