#include<cstdio>
#include<algorithm>
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int,int> PA;
const int N=200005,p=1000000007;
PA T[N],Z[N],FZ[N];
int ni[N],C[N],t[N],A[N],H[N],i,j,k,n,m,an,R;
char c;

inline char getc()
{
	#define VV 10000000
	static char s[VV],*l=s,*r=s;
	if (l==r)
		l=s,r=s+fread(s,1,VV,stdin);
	return l==r?0:*l++;
}

int read(){ int z=0; do c=getc(); while (c<'0'||c>'9'); while (c>='0'&&c<='9') z*=10,z+=c-'0',c=getc(); return z; }

void Plus(int &a,int b){ a+=b,a-=a<p?0:p; }
void Minus(int &a,int b){ a-=b,a+=a<0?p:0; }

PA operator *(const PA &A,const int &b)
{
	return b?PA((LL)A.fi*b%p,A.se):PA(A.fi,A.se+1);
}

void operator +=(PA &A,const PA &B)
{
	if (A.se<B.se)
		A=B;
	else
		if (A.se==B.se)
			Plus(A.fi,B.fi);
}

int power(int z,int k)
{
	int d=1;
	for (;k;z=(LL)z*z%p,k>>=1) if (k&1) d=(LL)d*z%p;
}

PA mt(int w,PA z)
{
	for (;w<=n;w+=w&-w) T[w]+=z;
}

PA qt(int w)
{
	PA z=PA(0,0);
	for (;w;w-=w&-w) z+=T[w];
	return z;
}

void add(int w){ for (;w<=n;w+=w&-w) ++t[w]; }
int getsum(int w){ int z=0; for (;w;w-=w&-w) z+=t[w]; return z; }

int solve()
{
	ni[2]=p+1>>1;
	n=read(),R=1;
	for (i=1;i<=n;++i) ++C[A[i]=read()];
	for (i=n;i;--i)
	{
		C[i]+=C[i+1],H[i]=C[i]-(n-i);
		if (H[i]<=0) return 0;
		R=(LL)R*H[i]%p;
	}
	for (ni[1]=1,i=2;i<=n;++i) ni[i]=p-(LL)(p/i)*ni[p%i]%p;
	ni[0]=0;
	Z[0]=FZ[0]=PA(1,0);
	for (i=1;i<=n;++i)
		Z[i]=Z[i-1]*((LL)(H[i]-1)*ni[H[i]]%p),
		FZ[i]=FZ[i-1]*((LL)H[i]*ni[H[i]-1]%p);
	
	for (i=1;i<=n;++i)
	{
		int d=A[i];
		PA D=qt(d);
		if (D.se==Z[d].se)
			Plus(an,(LL)D.fi*Z[d].fi%p);
		mt(d,FZ[d]);
	}
	for (i=1;i<=n;++i) T[i]=PA(0,0);
	for (i=n;i;--i)
	{
		int d=A[i];
		PA D=qt(d-1);
		if (D.se==Z[d].se)
			Minus(an,(LL)D.fi*Z[d].fi%p);
		mt(d,FZ[d]);
		Plus(an,(getsum(d-1)<<1)%p),
		add(d);
	}
	
	return (LL)an*R%p*ni[2]%p;
}

int main()
{
	printf("%d\n",solve());
	return 0;
}
### Prompt

In Cpp, your task is to solve 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<cstdio>
#include<algorithm>
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int,int> PA;
const int N=200005,p=1000000007;
PA T[N],Z[N],FZ[N];
int ni[N],C[N],t[N],A[N],H[N],i,j,k,n,m,an,R;
char c;

inline char getc()
{
	#define VV 10000000
	static char s[VV],*l=s,*r=s;
	if (l==r)
		l=s,r=s+fread(s,1,VV,stdin);
	return l==r?0:*l++;
}

int read(){ int z=0; do c=getc(); while (c<'0'||c>'9'); while (c>='0'&&c<='9') z*=10,z+=c-'0',c=getc(); return z; }

void Plus(int &a,int b){ a+=b,a-=a<p?0:p; }
void Minus(int &a,int b){ a-=b,a+=a<0?p:0; }

PA operator *(const PA &A,const int &b)
{
	return b?PA((LL)A.fi*b%p,A.se):PA(A.fi,A.se+1);
}

void operator +=(PA &A,const PA &B)
{
	if (A.se<B.se)
		A=B;
	else
		if (A.se==B.se)
			Plus(A.fi,B.fi);
}

int power(int z,int k)
{
	int d=1;
	for (;k;z=(LL)z*z%p,k>>=1) if (k&1) d=(LL)d*z%p;
}

PA mt(int w,PA z)
{
	for (;w<=n;w+=w&-w) T[w]+=z;
}

PA qt(int w)
{
	PA z=PA(0,0);
	for (;w;w-=w&-w) z+=T[w];
	return z;
}

void add(int w){ for (;w<=n;w+=w&-w) ++t[w]; }
int getsum(int w){ int z=0; for (;w;w-=w&-w) z+=t[w]; return z; }

int solve()
{
	ni[2]=p+1>>1;
	n=read(),R=1;
	for (i=1;i<=n;++i) ++C[A[i]=read()];
	for (i=n;i;--i)
	{
		C[i]+=C[i+1],H[i]=C[i]-(n-i);
		if (H[i]<=0) return 0;
		R=(LL)R*H[i]%p;
	}
	for (ni[1]=1,i=2;i<=n;++i) ni[i]=p-(LL)(p/i)*ni[p%i]%p;
	ni[0]=0;
	Z[0]=FZ[0]=PA(1,0);
	for (i=1;i<=n;++i)
		Z[i]=Z[i-1]*((LL)(H[i]-1)*ni[H[i]]%p),
		FZ[i]=FZ[i-1]*((LL)H[i]*ni[H[i]-1]%p);
	
	for (i=1;i<=n;++i)
	{
		int d=A[i];
		PA D=qt(d);
		if (D.se==Z[d].se)
			Plus(an,(LL)D.fi*Z[d].fi%p);
		mt(d,FZ[d]);
	}
	for (i=1;i<=n;++i) T[i]=PA(0,0);
	for (i=n;i;--i)
	{
		int d=A[i];
		PA D=qt(d-1);
		if (D.se==Z[d].se)
			Minus(an,(LL)D.fi*Z[d].fi%p);
		mt(d,FZ[d]);
		Plus(an,(getsum(d-1)<<1)%p),
		add(d);
	}
	
	return (LL)an*R%p*ni[2]%p;
}

int main()
{
	printf("%d\n",solve());
	return 0;
}
```