#include<bits/stdc++.h>
using namespace std;
#define cs const
#define pb push_back
#define ll long long 
#define pii pair<int,int>
#define fi first
#define se second
#define bg begin
cs int RLEN=(1<<20)|3;
inline char gc(){
	static char ibuf[RLEN],*ib,*ob;
	(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
	return (ib==ob)?EOF:*ib++;
}
inline int read(){
	char ch=gc();
	int res=0;bool f=1;
	while(!isdigit(ch))f^=ch=='-',ch=gc();
	while(isdigit(ch))res=(res*10)+(ch^48),ch=gc();
	return f?res:-res;
}
inline ll readll(){
	char ch=gc();
	ll res=0;bool f=1;
	while(!isdigit(ch))f^=ch=='-',ch=gc();
	while(isdigit(ch))res=(res*10)+(ch^48),ch=gc();
	return f?res:-res;	
}
inline int readstring(char *s){
	char ch=gc();int l=0;
	while(isspace(ch))ch=gc();
	while(!isspace(ch)&&ch!=EOF)s[++l]=ch,ch=gc();
	return l;
}
inline char readchar(){
	char ch=gc();
	while(isspace(ch))ch=gc();return ch;
}
template<typename tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<typename tp>inline void chemn(tp &a,tp b){a<b?a=b:0;}
cs int mod=1e9+7;
inline int add(int a,int b){return (a+b>=mod)?(a+b-mod):(a+b);}
inline int dec(int a,int b){return (a<b)?(a-b+mod):(a-b);}
inline int mul(int a,int b){static ll r;r=(ll)a*b;return (r>=mod)?(r%mod):r;}
inline void Add(int &a,int b){a=(a+b>=mod)?(a+b-mod):(a+b);}
inline void Dec(int &a,int b){a=(a<b)?(a-b+mod):(a-b);}
inline void Mul(int &a,int b){static ll r;r=(ll)a*b;a=(r>=mod)?(r%mod):r;}
inline int ksm(int a,int b,int res=1){for(;b;b>>=1,Mul(a,a))(b&1)&&(Mul(res,a),1);return res;}
inline int Inv(int x){return ksm(x,mod-2);} 
cs int N=200005,iv2=Inv(2);
int n,a[N],cnt[N],pre[N],sm[N],is[N];
namespace bit{
	int tr1[N],tr2[N];
	#define lb(x) (x&(-x))
	void update(int p,int k){for(;p<=n;p+=lb(p))Add(tr1[p],k),tr2[p]++;}
	int query(int p,int res=0){for(;p>0;p-=lb(p))Add(res,tr1[p]);return res;}
	int query2(int p,int res=0){for(;p>0;p-=lb(p))Add(res,tr2[p]);return res;}
	void clear(){memset(tr1,0,sizeof(tr1)),memset(tr2,0,sizeof(tr2));}
	#undef lb
}
int main(){
	#ifdef Stargazer
	freopen("lx.in","r",stdin);
	#endif
	n=read();
	for(int i=1;i<=n;i++)a[i]=read(),cnt[a[i]]++;
	for(int i=n;i;i--)cnt[i]+=cnt[i+1];int all=1;
	for(int i=1;i<=n;i++)cnt[i]-=n-i,Mul(all,cnt[i]);
//	cout<<all<<'\n';
	sm[0]=1,pre[0]=1;
	for(int i=1;i<=n;i++){
		int vl=mul(cnt[i]-1,Inv(cnt[i]));
		if(!vl)pre[i]=i,sm[i]=sm[i-1];
		else pre[i]=pre[i-1],sm[i]=mul(sm[i-1],vl);
		is[i]=Inv(sm[i]);
	}int res=0;
	for(int i=1;i<=n;i++){
		Add(res,mul(sm[a[i]],mul(all,mul(iv2,dec(bit::query(a[i]),bit::query(pre[a[i]]-1))))));
		bit::update(a[i],is[a[i]]);
	}
	bit::clear();
	for(int i=n;i;i--){
		Dec(res,mul(sm[a[i]],mul(all,mul(iv2,dec(bit::query(a[i]-1),bit::query(pre[a[i]]-1))))));
		Add(res,mul(all,bit::query2(a[i]-1)));
		bit::update(a[i],is[a[i]]);
	}cout<<res<<'\n';return 0;
}
### Prompt

Develop a solution in cpp to the problem described below:

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;
#define cs const
#define pb push_back
#define ll long long 
#define pii pair<int,int>
#define fi first
#define se second
#define bg begin
cs int RLEN=(1<<20)|3;
inline char gc(){
	static char ibuf[RLEN],*ib,*ob;
	(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
	return (ib==ob)?EOF:*ib++;
}
inline int read(){
	char ch=gc();
	int res=0;bool f=1;
	while(!isdigit(ch))f^=ch=='-',ch=gc();
	while(isdigit(ch))res=(res*10)+(ch^48),ch=gc();
	return f?res:-res;
}
inline ll readll(){
	char ch=gc();
	ll res=0;bool f=1;
	while(!isdigit(ch))f^=ch=='-',ch=gc();
	while(isdigit(ch))res=(res*10)+(ch^48),ch=gc();
	return f?res:-res;	
}
inline int readstring(char *s){
	char ch=gc();int l=0;
	while(isspace(ch))ch=gc();
	while(!isspace(ch)&&ch!=EOF)s[++l]=ch,ch=gc();
	return l;
}
inline char readchar(){
	char ch=gc();
	while(isspace(ch))ch=gc();return ch;
}
template<typename tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<typename tp>inline void chemn(tp &a,tp b){a<b?a=b:0;}
cs int mod=1e9+7;
inline int add(int a,int b){return (a+b>=mod)?(a+b-mod):(a+b);}
inline int dec(int a,int b){return (a<b)?(a-b+mod):(a-b);}
inline int mul(int a,int b){static ll r;r=(ll)a*b;return (r>=mod)?(r%mod):r;}
inline void Add(int &a,int b){a=(a+b>=mod)?(a+b-mod):(a+b);}
inline void Dec(int &a,int b){a=(a<b)?(a-b+mod):(a-b);}
inline void Mul(int &a,int b){static ll r;r=(ll)a*b;a=(r>=mod)?(r%mod):r;}
inline int ksm(int a,int b,int res=1){for(;b;b>>=1,Mul(a,a))(b&1)&&(Mul(res,a),1);return res;}
inline int Inv(int x){return ksm(x,mod-2);} 
cs int N=200005,iv2=Inv(2);
int n,a[N],cnt[N],pre[N],sm[N],is[N];
namespace bit{
	int tr1[N],tr2[N];
	#define lb(x) (x&(-x))
	void update(int p,int k){for(;p<=n;p+=lb(p))Add(tr1[p],k),tr2[p]++;}
	int query(int p,int res=0){for(;p>0;p-=lb(p))Add(res,tr1[p]);return res;}
	int query2(int p,int res=0){for(;p>0;p-=lb(p))Add(res,tr2[p]);return res;}
	void clear(){memset(tr1,0,sizeof(tr1)),memset(tr2,0,sizeof(tr2));}
	#undef lb
}
int main(){
	#ifdef Stargazer
	freopen("lx.in","r",stdin);
	#endif
	n=read();
	for(int i=1;i<=n;i++)a[i]=read(),cnt[a[i]]++;
	for(int i=n;i;i--)cnt[i]+=cnt[i+1];int all=1;
	for(int i=1;i<=n;i++)cnt[i]-=n-i,Mul(all,cnt[i]);
//	cout<<all<<'\n';
	sm[0]=1,pre[0]=1;
	for(int i=1;i<=n;i++){
		int vl=mul(cnt[i]-1,Inv(cnt[i]));
		if(!vl)pre[i]=i,sm[i]=sm[i-1];
		else pre[i]=pre[i-1],sm[i]=mul(sm[i-1],vl);
		is[i]=Inv(sm[i]);
	}int res=0;
	for(int i=1;i<=n;i++){
		Add(res,mul(sm[a[i]],mul(all,mul(iv2,dec(bit::query(a[i]),bit::query(pre[a[i]]-1))))));
		bit::update(a[i],is[a[i]]);
	}
	bit::clear();
	for(int i=n;i;i--){
		Dec(res,mul(sm[a[i]],mul(all,mul(iv2,dec(bit::query(a[i]-1),bit::query(pre[a[i]]-1))))));
		Add(res,mul(all,bit::query2(a[i]-1)));
		bit::update(a[i],is[a[i]]);
	}cout<<res<<'\n';return 0;
}
```