#include<bits/stdc++.h>
#include<algorithm>
using namespace std;
#define ll long long
#define llu unsigned long long
#define ld double
#define llu unsigned long long
#define rep(i,x,y)  for(int i=int(x);i<int(y);++i)
#define For(i,x,y)  for(int i=int(x);i<=int(y);++i)
#define FOr(i,x,y)  for(int i=int(x);i>=int(y);--i)
#define pi acos(-1) 
#define mk make_pair
#define pa pair<ll,ll>
#define lf else if
#define IL inline
#define max(x,y)    ((x)<(y)?(y):(x))
#define min(x,y)    ((x)<(y)?(x):(y))
#define sqr(x)      ((x)*(x))
#define Mul(x,y)    ((x)=1LL*(x)*(y)%mod)
#define Add(x,y)    ((x)=((x)+(y))%mod)
#define E(x)        return writeln(x),0
#define LL			(long long)
#define p(x)        printf("~%lld~\n",LL(x))
#define pp(x,y)     printf("~~%lld %lld~~\n",LL(x),LL(y))
#define ppp(x,y,z)  printf("~~~%lld %lld %lld~~~\n",LL(x),LL(y),LL(z))
#define pppp(a,b,c,d)	printf("~~~%lld %lld %lld %lld\n",LL(a),LL(b),LL(c),LL(d))
#define f_in(x)     freopen(x".in","r",stdin)
#define f_out(x)    freopen(x".out","w",stdout)
#define open(x)     f_in(x),f_out(x)
#define fi first
#define se second
#define GuYue		puts("\nGuYueNa________________________________________________________________________________")
#define y1			fafa____
typedef complex<double> E;
namespace SHENZHEBEI{
#ifdef LOCAL
    struct _{_(){freopen("cf.in","r",stdin);}}_; 
#endif 
#define NEG 1
    static const int GYN=2333333;
    char SZB[GYN],*S=SZB,*T=SZB;
    inline char gc(){   if (S==T){  T=(S=SZB)+fread(SZB,1,GYN,stdin); if (S==T) return '\n';  }   return *S++;    }
#if NEG
    inline ll read(){    ll x=0,g=1; char ch=gc();   for (;!isdigit(ch);ch=gc()) if (ch=='-') g=-1;  for (;isdigit(ch);ch=gc())  x=x*10-48+ch;   return x*g; }
    inline void write(ll x){    if (x<0) putchar('-'),x=-x;  if (x>=10)   write(x/10);    putchar(x%10+'0');  }
#else
    inline ll read(){    ll x=0; char ch=gc();   for (;!isdigit(ch);ch=gc());    for (;isdigit(ch);ch=gc())  x=x*10-48+ch;   return x;   }
    inline void write(ll x){    if (x>=10)   write(x/10);    putchar(x%10+'0');  }
#endif
    inline char readchar(){ char ch=gc();   for(;isspace(ch);ch=gc());  return ch;  }
    inline ll readstr(char *s){ char ch=gc();   int cur=0;  for(;isspace(ch);ch=gc());      for(;!isspace(ch);ch=gc())  s[cur++]=ch;    s[cur]='\0';    return cur; }
	void Print(long long *a,int s,int t){For(i,s,t)printf("%lld ",a[i]);puts("");}
	void Print(int *a,int s,int t){For(i,s,t)printf("%d ",a[i]);puts("");}
	void Print(char *a,int s,int t){For(i,s,t)putchar(a[i]);puts("");}
	void writeln(ll x){write(x);putchar('\n');}
}using namespace SHENZHEBEI;
const ll N=1000010,mod=1e9+7,Inv2=(mod+1)/2;
ll nxt[N],c[N],Dec_Pre[N],True_Suc[N],True_Pre[N],a[N],cnt[N],n,answ;
ll ppow(ll x,ll k){ll ans=1;for(;k;k>>=1,Mul(x,x))if (k&1)Mul(ans,x);return ans;}
void add(ll x,ll v){for(;x<=n;x+=x&-x)Add(c[x],v);}
ll ask(ll x){ll ans=0;for(;x;x-=x&-x)Add(ans,c[x]);return ans;} 
ll Qsum(ll x,ll y){return (ask(y)-ask(x-1))%mod;}
int main(){
	n=read();
	For(i,1,n)a[i]=read(),++cnt[a[i]];
	cnt[n+1]=1;FOr(i,n,1)cnt[i]+=cnt[i+1]-1;
	True_Pre[0]=True_Pre[n+1]=Dec_Pre[0]=Dec_Pre[n+1]=True_Suc[0]=True_Suc[n+1]=1;
	For(i,1,n)if (cnt[i]<=0)return puts("0"),0;
	For(i,1,n)	Dec_Pre[i]=max(cnt[i]-1,1ll)*Dec_Pre[i-1]%mod,
				True_Pre[i]=cnt[i]*True_Pre[i-1]%mod;
	FOr(i,n,1)	True_Suc[i]=cnt[i]*True_Suc[i+1]%mod;
	nxt[n+1]=n+1;FOr(i,n,1)nxt[i]=cnt[i]==1?i:nxt[i+1];
//	For(i,1,n)writeln(nxt[i]);
	For(i,1,n){
		ll sum=Qsum(a[i]+1,nxt[a[i]]-1);
//		pp(i,nxt[a[i]]-1);
		Add(answ,-sum*ppow(Dec_Pre[a[i]],mod-2)%mod*True_Pre[a[i]]);
		add(a[i],Dec_Pre[a[i]]*True_Suc[a[i]+1]);
	}
	memset(c,0,sizeof c);
	FOr(i,n,1){
		ll sum=Qsum(a[i],nxt[a[i]]-1);
		Add(answ,sum*ppow(Dec_Pre[a[i]],mod-2)%mod*True_Pre[a[i]]);
		add(a[i],Dec_Pre[a[i]]*True_Suc[a[i]+1]);
	}
	Mul(answ,Inv2);
	memset(c,0,sizeof c);
	For(i,1,n){
		ll sum=Qsum(a[i]+1,n);
		Add(answ,True_Suc[1]*sum);
		add(a[i],1);
	}writeln((answ+mod)%mod);
}
/*
每次增加一个权值是**的。。
a[i]>a[j]

a[i]<=a[j]

[1]		[2]		[i]		[i+1]-1	[j]-1	[6]

2 3 3

1	3	2
2	3	1


*/
### Prompt

Your challenge is to write 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>
#include<algorithm>
using namespace std;
#define ll long long
#define llu unsigned long long
#define ld double
#define llu unsigned long long
#define rep(i,x,y)  for(int i=int(x);i<int(y);++i)
#define For(i,x,y)  for(int i=int(x);i<=int(y);++i)
#define FOr(i,x,y)  for(int i=int(x);i>=int(y);--i)
#define pi acos(-1) 
#define mk make_pair
#define pa pair<ll,ll>
#define lf else if
#define IL inline
#define max(x,y)    ((x)<(y)?(y):(x))
#define min(x,y)    ((x)<(y)?(x):(y))
#define sqr(x)      ((x)*(x))
#define Mul(x,y)    ((x)=1LL*(x)*(y)%mod)
#define Add(x,y)    ((x)=((x)+(y))%mod)
#define E(x)        return writeln(x),0
#define LL			(long long)
#define p(x)        printf("~%lld~\n",LL(x))
#define pp(x,y)     printf("~~%lld %lld~~\n",LL(x),LL(y))
#define ppp(x,y,z)  printf("~~~%lld %lld %lld~~~\n",LL(x),LL(y),LL(z))
#define pppp(a,b,c,d)	printf("~~~%lld %lld %lld %lld\n",LL(a),LL(b),LL(c),LL(d))
#define f_in(x)     freopen(x".in","r",stdin)
#define f_out(x)    freopen(x".out","w",stdout)
#define open(x)     f_in(x),f_out(x)
#define fi first
#define se second
#define GuYue		puts("\nGuYueNa________________________________________________________________________________")
#define y1			fafa____
typedef complex<double> E;
namespace SHENZHEBEI{
#ifdef LOCAL
    struct _{_(){freopen("cf.in","r",stdin);}}_; 
#endif 
#define NEG 1
    static const int GYN=2333333;
    char SZB[GYN],*S=SZB,*T=SZB;
    inline char gc(){   if (S==T){  T=(S=SZB)+fread(SZB,1,GYN,stdin); if (S==T) return '\n';  }   return *S++;    }
#if NEG
    inline ll read(){    ll x=0,g=1; char ch=gc();   for (;!isdigit(ch);ch=gc()) if (ch=='-') g=-1;  for (;isdigit(ch);ch=gc())  x=x*10-48+ch;   return x*g; }
    inline void write(ll x){    if (x<0) putchar('-'),x=-x;  if (x>=10)   write(x/10);    putchar(x%10+'0');  }
#else
    inline ll read(){    ll x=0; char ch=gc();   for (;!isdigit(ch);ch=gc());    for (;isdigit(ch);ch=gc())  x=x*10-48+ch;   return x;   }
    inline void write(ll x){    if (x>=10)   write(x/10);    putchar(x%10+'0');  }
#endif
    inline char readchar(){ char ch=gc();   for(;isspace(ch);ch=gc());  return ch;  }
    inline ll readstr(char *s){ char ch=gc();   int cur=0;  for(;isspace(ch);ch=gc());      for(;!isspace(ch);ch=gc())  s[cur++]=ch;    s[cur]='\0';    return cur; }
	void Print(long long *a,int s,int t){For(i,s,t)printf("%lld ",a[i]);puts("");}
	void Print(int *a,int s,int t){For(i,s,t)printf("%d ",a[i]);puts("");}
	void Print(char *a,int s,int t){For(i,s,t)putchar(a[i]);puts("");}
	void writeln(ll x){write(x);putchar('\n');}
}using namespace SHENZHEBEI;
const ll N=1000010,mod=1e9+7,Inv2=(mod+1)/2;
ll nxt[N],c[N],Dec_Pre[N],True_Suc[N],True_Pre[N],a[N],cnt[N],n,answ;
ll ppow(ll x,ll k){ll ans=1;for(;k;k>>=1,Mul(x,x))if (k&1)Mul(ans,x);return ans;}
void add(ll x,ll v){for(;x<=n;x+=x&-x)Add(c[x],v);}
ll ask(ll x){ll ans=0;for(;x;x-=x&-x)Add(ans,c[x]);return ans;} 
ll Qsum(ll x,ll y){return (ask(y)-ask(x-1))%mod;}
int main(){
	n=read();
	For(i,1,n)a[i]=read(),++cnt[a[i]];
	cnt[n+1]=1;FOr(i,n,1)cnt[i]+=cnt[i+1]-1;
	True_Pre[0]=True_Pre[n+1]=Dec_Pre[0]=Dec_Pre[n+1]=True_Suc[0]=True_Suc[n+1]=1;
	For(i,1,n)if (cnt[i]<=0)return puts("0"),0;
	For(i,1,n)	Dec_Pre[i]=max(cnt[i]-1,1ll)*Dec_Pre[i-1]%mod,
				True_Pre[i]=cnt[i]*True_Pre[i-1]%mod;
	FOr(i,n,1)	True_Suc[i]=cnt[i]*True_Suc[i+1]%mod;
	nxt[n+1]=n+1;FOr(i,n,1)nxt[i]=cnt[i]==1?i:nxt[i+1];
//	For(i,1,n)writeln(nxt[i]);
	For(i,1,n){
		ll sum=Qsum(a[i]+1,nxt[a[i]]-1);
//		pp(i,nxt[a[i]]-1);
		Add(answ,-sum*ppow(Dec_Pre[a[i]],mod-2)%mod*True_Pre[a[i]]);
		add(a[i],Dec_Pre[a[i]]*True_Suc[a[i]+1]);
	}
	memset(c,0,sizeof c);
	FOr(i,n,1){
		ll sum=Qsum(a[i],nxt[a[i]]-1);
		Add(answ,sum*ppow(Dec_Pre[a[i]],mod-2)%mod*True_Pre[a[i]]);
		add(a[i],Dec_Pre[a[i]]*True_Suc[a[i]+1]);
	}
	Mul(answ,Inv2);
	memset(c,0,sizeof c);
	For(i,1,n){
		ll sum=Qsum(a[i]+1,n);
		Add(answ,True_Suc[1]*sum);
		add(a[i],1);
	}writeln((answ+mod)%mod);
}
/*
每次增加一个权值是**的。。
a[i]>a[j]

a[i]<=a[j]

[1]		[2]		[i]		[i+1]-1	[j]-1	[6]

2 3 3

1	3	2
2	3	1


*/
```