//by yjz
#include<bits/stdc++.h>
using namespace std;
#define FF first
#define SS second
#define PB push_back
#define MP make_pair
#define foreach(it,s) for(__typeof((s).begin()) it=(s).begin();it!=(s).end();it++)
#ifndef LOCAL
#define cerr if(0)cout
#endif
typedef long long ll;
const int mod=1e9+7;
//My i/o stream
struct fastio
{
	char s[100000];
	int it, len;
	fastio() {it=len=0;}
	inline char get()
	{
		if (it<len) return s[it++]; it=0;
		len=fread(s, 1, 100000, stdin);
		if (len==0) return EOF; else return s[it++];
	}
	bool notend()
	{
		char c=get();
		while (c==' '||c=='\n') c=get();
		if (it>0) it--;
		return c!=EOF;
	}
}_buff;
#define geti(x) x=getnum()
#define getii(x,y) geti(x),geti(y)
#define getiii(x,y,z) getii(x,y),geti(z)
#define puti(x) putnum(x),putchar(' ')
#define putii(x,y) puti(x),puti(y)
#define putiii(x,y,z) putii(x,y),puti(z)
#define putsi(x) putnum(x),putchar('\n')
#define putsii(x,y) puti(x),putsi(y)
#define putsiii(x,y,z) putii(x,y),putsi(z)
inline ll getnum()
{
	ll r=0; bool ng=0; char c; c=_buff.get();
	while (c!='-'&&(c<'0'||c>'9')) c=_buff.get();
	if (c=='-') ng=1, c=_buff.get();
	while (c>='0'&&c<='9') r=r*10+c-'0', c=_buff.get();
	return ng?-r:r;
}
template <class T> inline void putnum(T x)
{
	if (x<0) putchar('-'), x=-x;
	register short a[20]={}, sz=0;
	while (x) a[sz++]=x%10, x/=10;
	if(sz==0) putchar('0');
	for (int i=sz-1; i>=0; i--) putchar('0'+a[i]);
}
inline char getreal() {char c=_buff.get(); while (c<=32) c=_buff.get(); return c;}
ll qpow(ll x, ll k) {return k==0? 1: 1ll*qpow(1ll*x*x%mod,k>>1)*(k&1?x:1)%mod;}
const int maxn = 200111;
struct SGT
{
	struct node
	{
		ll lz;
		ll sum;
		void addlz(ll L)
		{
			lz = lz*L%mod;
			sum = sum*L%mod;
		}
	}a[maxn*4];
	#define ls p<<1
	#define rs p<<1|1
	void pushdown(int p)
	{
		if (a[p].lz!=1)
		{
			a[ls].addlz(a[p].lz);
			a[rs].addlz(a[p].lz);
			a[p].lz = 1;
		}
	}
	void update(int p)
	{
		a[p].sum = (a[ls].sum+a[rs].sum)%mod;
	}
	void modify(int x, ll v, int l, int r, int p)
	{
		if (r<x||x<l) return;
		if (l==r)
		{
			a[p].sum = v;
			return;
		}
		pushdown(p);
		int m = l+r>>1;
		modify(x, v, l, m, ls);
		modify(x, v, m+1, r, rs);
		update(p);
	}
	ll query(int x, int y, int l, int r, int p)
	{
		if (r<x||y<l) return 0;
		if (x<=l&&r<=y) return a[p].sum;
		pushdown(p);
		int m = l+r>>1;
		return (query(x, y, l, m, ls)+query(x, y, m+1, r, rs))%mod;
	}
}yxg;
int n;
pair<int,int> a[maxn];
ll sprod[maxn], prod[maxn];
int tab[maxn];
void inc(int x)
{
	for (int i=x; i<maxn; i+=i&(-i)) tab[i]++;
}
int query(int x)
{
	int ret = 0;
	for (int i=x; i; i-=i&(-i)) ret += tab[i];
	return ret;
}
int main()
{
	geti(n);
	for (int i=1; i<=n; i++) geti(a[i].FF), a[i].SS = i;
	sort(a+1, a+n+1);
	sprod[n+1] = 1;
	for (int i=n; i>=1; i--) sprod[i] = sprod[i+1]*(a[i].FF-i+1)%mod;
	prod[0] = 1;
	for (int i=1; i<=n; i++) prod[i] = prod[i-1]*(a[i].FF-i+1)%mod;
	ll tot = 1;
	for (int i=1; i<=n; i++) tot = tot*(a[i].FF-i+1)%mod;
	ll dans = 0;
	for (int i=1; i<=n; i++)
	{
		ll tmp = query(a[i].SS);
		dans += (-tmp*tot+(i-1-tmp)*tot)%mod;
		inc(a[i].SS);
		
		dans -= -1ll*yxg.query(1, a[i].SS-1, 1, n, 1)*sprod[i+1]%mod;
		dans += -1ll*yxg.query(a[i].SS+1, n, 1, n, 1)*sprod[i+1]%mod;
		yxg.modify(a[i].SS, prod[i], 1, n, 1);
		yxg.a[1].addlz(a[i].FF-i);
	}
	cout<<((tot*((1ll*n*(n-1)/2)%mod)%mod+dans%mod)*qpow(2, mod-2)%mod+mod)%mod<<endl;
	return 0;
}

### Prompt

Please provide a Cpp coded solution 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
//by yjz
#include<bits/stdc++.h>
using namespace std;
#define FF first
#define SS second
#define PB push_back
#define MP make_pair
#define foreach(it,s) for(__typeof((s).begin()) it=(s).begin();it!=(s).end();it++)
#ifndef LOCAL
#define cerr if(0)cout
#endif
typedef long long ll;
const int mod=1e9+7;
//My i/o stream
struct fastio
{
	char s[100000];
	int it, len;
	fastio() {it=len=0;}
	inline char get()
	{
		if (it<len) return s[it++]; it=0;
		len=fread(s, 1, 100000, stdin);
		if (len==0) return EOF; else return s[it++];
	}
	bool notend()
	{
		char c=get();
		while (c==' '||c=='\n') c=get();
		if (it>0) it--;
		return c!=EOF;
	}
}_buff;
#define geti(x) x=getnum()
#define getii(x,y) geti(x),geti(y)
#define getiii(x,y,z) getii(x,y),geti(z)
#define puti(x) putnum(x),putchar(' ')
#define putii(x,y) puti(x),puti(y)
#define putiii(x,y,z) putii(x,y),puti(z)
#define putsi(x) putnum(x),putchar('\n')
#define putsii(x,y) puti(x),putsi(y)
#define putsiii(x,y,z) putii(x,y),putsi(z)
inline ll getnum()
{
	ll r=0; bool ng=0; char c; c=_buff.get();
	while (c!='-'&&(c<'0'||c>'9')) c=_buff.get();
	if (c=='-') ng=1, c=_buff.get();
	while (c>='0'&&c<='9') r=r*10+c-'0', c=_buff.get();
	return ng?-r:r;
}
template <class T> inline void putnum(T x)
{
	if (x<0) putchar('-'), x=-x;
	register short a[20]={}, sz=0;
	while (x) a[sz++]=x%10, x/=10;
	if(sz==0) putchar('0');
	for (int i=sz-1; i>=0; i--) putchar('0'+a[i]);
}
inline char getreal() {char c=_buff.get(); while (c<=32) c=_buff.get(); return c;}
ll qpow(ll x, ll k) {return k==0? 1: 1ll*qpow(1ll*x*x%mod,k>>1)*(k&1?x:1)%mod;}
const int maxn = 200111;
struct SGT
{
	struct node
	{
		ll lz;
		ll sum;
		void addlz(ll L)
		{
			lz = lz*L%mod;
			sum = sum*L%mod;
		}
	}a[maxn*4];
	#define ls p<<1
	#define rs p<<1|1
	void pushdown(int p)
	{
		if (a[p].lz!=1)
		{
			a[ls].addlz(a[p].lz);
			a[rs].addlz(a[p].lz);
			a[p].lz = 1;
		}
	}
	void update(int p)
	{
		a[p].sum = (a[ls].sum+a[rs].sum)%mod;
	}
	void modify(int x, ll v, int l, int r, int p)
	{
		if (r<x||x<l) return;
		if (l==r)
		{
			a[p].sum = v;
			return;
		}
		pushdown(p);
		int m = l+r>>1;
		modify(x, v, l, m, ls);
		modify(x, v, m+1, r, rs);
		update(p);
	}
	ll query(int x, int y, int l, int r, int p)
	{
		if (r<x||y<l) return 0;
		if (x<=l&&r<=y) return a[p].sum;
		pushdown(p);
		int m = l+r>>1;
		return (query(x, y, l, m, ls)+query(x, y, m+1, r, rs))%mod;
	}
}yxg;
int n;
pair<int,int> a[maxn];
ll sprod[maxn], prod[maxn];
int tab[maxn];
void inc(int x)
{
	for (int i=x; i<maxn; i+=i&(-i)) tab[i]++;
}
int query(int x)
{
	int ret = 0;
	for (int i=x; i; i-=i&(-i)) ret += tab[i];
	return ret;
}
int main()
{
	geti(n);
	for (int i=1; i<=n; i++) geti(a[i].FF), a[i].SS = i;
	sort(a+1, a+n+1);
	sprod[n+1] = 1;
	for (int i=n; i>=1; i--) sprod[i] = sprod[i+1]*(a[i].FF-i+1)%mod;
	prod[0] = 1;
	for (int i=1; i<=n; i++) prod[i] = prod[i-1]*(a[i].FF-i+1)%mod;
	ll tot = 1;
	for (int i=1; i<=n; i++) tot = tot*(a[i].FF-i+1)%mod;
	ll dans = 0;
	for (int i=1; i<=n; i++)
	{
		ll tmp = query(a[i].SS);
		dans += (-tmp*tot+(i-1-tmp)*tot)%mod;
		inc(a[i].SS);
		
		dans -= -1ll*yxg.query(1, a[i].SS-1, 1, n, 1)*sprod[i+1]%mod;
		dans += -1ll*yxg.query(a[i].SS+1, n, 1, n, 1)*sprod[i+1]%mod;
		yxg.modify(a[i].SS, prod[i], 1, n, 1);
		yxg.a[1].addlz(a[i].FF-i);
	}
	cout<<((tot*((1ll*n*(n-1)/2)%mod)%mod+dans%mod)*qpow(2, mod-2)%mod+mod)%mod<<endl;
	return 0;
}

```