#include <bits/stdc++.h>
#define meow(args...) fprintf(stderr, args)
typedef unsigned u32;
typedef long long s64;
typedef unsigned long long u64;
template<class T1, class T2> inline bool cmin(T1 &a, const T2 &b) {return b<a?(a=b, true):false;}
template<class T1, class T2> inline bool cmax(T1 &a, const T2 &b) {return a<b?(a=b, true):false;}
template<class Type> Type read() {
	Type a;
	bool b;
	unsigned char c;
	while(c=getchar()-48, (c>9)&(c!=253));
	for(a=(b=c==253)?0:c; (c=getchar()-48)<=9; a=a*10+c);
	return b?-a:a;
}
int (*rd)()=read<int>;
const u32 P=1e9+7;
inline u32 &inc(u32 &a, u32 b) {return (a+=b)<P?a:(a-=P);}
inline u32 &dec(u32 &a, u32 b) {return (a-=b)&0x80000000?(a+=P):a;}
inline u32 sum(u32 a, u32 b) {return (a+=b)<P?a:a-P;}
inline u32 dif(u32 a, u32 b) {return (a-=b)&0x80000000?a+P:a;}
u64 power(u64 a, int b) {
	u64 ans=1;
	for(; b; a=a*a%P, b/=2) if(b&1) ans=ans*a%P;
	return ans;
}
const int N=2e5+1;
int n, a[N], b[N], cnt[N], rank[N];
u32 bit[N], p[N], invp[N], q[N], invq[N], u[N], v[N];
u32 query(int x) {
	u32 ans=0;
	for(; x; x&=x-1) inc(ans, bit[x]);
	return ans;
}
u32 add(int x, u32 v) {
	for(; x<=n; x+=x&-x) inc(bit[x], v);
}
void clear(int x) {
	for(; x<=n; x+=x&-x) bit[x]=0;
}
int main() {
	n=rd();
	for(int i=1; i<=n; ++i) ++cnt[a[i]=rd()];
	for(int i=1; i<=n; ++i) cnt[i]+=cnt[i-1];
	for(int i=n; i>=1; --i) rank[cnt[a[i]]]=i, b[cnt[a[i]]--]=a[i];
	memset(cnt+1, 0, n*sizeof(int));
	p[0]=q[0]=1;
	for(int i=1; i<=n; ++i) p[i]=p[i-1]*(b[i]-i+1llu)%P;
	if(p[n]==0) puts("0"), exit(0);
	for(int i=1; i<=n; ++i) cnt[i]=cnt[i-1]+cmax(u[i]=b[i]-i, 1), q[i]=(u64)q[i-1]*u[i]%P;
	invp[n]=power(p[n], P-2), invq[n]=power(q[n], P-2);
	for(int i=n; i>=1; --i) invp[i-1]=invp[i]*(b[i]-i+1llu)%P;
	for(int i=n; i>=1; --i) invq[i-1]=(u64)invq[i]*u[i]%P;
	u64 invcnt=0;
	for(int i=1; i<=n; ++i) {
		for(int j=a[i]+1; j<=n; j+=j&-j) invcnt+=bit[j];
		for(int j=a[i]; j; j&=j-1) ++bit[j];
	}
	u32 ans=invcnt*2%P;
	memset(bit+1, 0, n*sizeof(int));
	for(int i=1; i<=n; ++i) u[i]=(u64)p[i-1]*invq[i-1]%P*(b[i]-i+1llu)%P;
	for(int i=1; i<=n; ++i) v[i]=(u64)q[i-1]*invp[i]%P;
	for(int i=1, j=0; i<=n; ++i) {
		if(i>1&&b[i-1]==i-1) {
			while(j<i-1) clear(rank[++j]);
		} else {
			ans=(ans+(2llu*query(rank[i]-1)+P-query(n))*v[i])%P;
		}
		add(rank[i], u[i]);
	}
	ans=(ans+(ans&1?P:0llu))/2*p[n]%P;
	printf("%u\n", ans);
	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>
#define meow(args...) fprintf(stderr, args)
typedef unsigned u32;
typedef long long s64;
typedef unsigned long long u64;
template<class T1, class T2> inline bool cmin(T1 &a, const T2 &b) {return b<a?(a=b, true):false;}
template<class T1, class T2> inline bool cmax(T1 &a, const T2 &b) {return a<b?(a=b, true):false;}
template<class Type> Type read() {
	Type a;
	bool b;
	unsigned char c;
	while(c=getchar()-48, (c>9)&(c!=253));
	for(a=(b=c==253)?0:c; (c=getchar()-48)<=9; a=a*10+c);
	return b?-a:a;
}
int (*rd)()=read<int>;
const u32 P=1e9+7;
inline u32 &inc(u32 &a, u32 b) {return (a+=b)<P?a:(a-=P);}
inline u32 &dec(u32 &a, u32 b) {return (a-=b)&0x80000000?(a+=P):a;}
inline u32 sum(u32 a, u32 b) {return (a+=b)<P?a:a-P;}
inline u32 dif(u32 a, u32 b) {return (a-=b)&0x80000000?a+P:a;}
u64 power(u64 a, int b) {
	u64 ans=1;
	for(; b; a=a*a%P, b/=2) if(b&1) ans=ans*a%P;
	return ans;
}
const int N=2e5+1;
int n, a[N], b[N], cnt[N], rank[N];
u32 bit[N], p[N], invp[N], q[N], invq[N], u[N], v[N];
u32 query(int x) {
	u32 ans=0;
	for(; x; x&=x-1) inc(ans, bit[x]);
	return ans;
}
u32 add(int x, u32 v) {
	for(; x<=n; x+=x&-x) inc(bit[x], v);
}
void clear(int x) {
	for(; x<=n; x+=x&-x) bit[x]=0;
}
int main() {
	n=rd();
	for(int i=1; i<=n; ++i) ++cnt[a[i]=rd()];
	for(int i=1; i<=n; ++i) cnt[i]+=cnt[i-1];
	for(int i=n; i>=1; --i) rank[cnt[a[i]]]=i, b[cnt[a[i]]--]=a[i];
	memset(cnt+1, 0, n*sizeof(int));
	p[0]=q[0]=1;
	for(int i=1; i<=n; ++i) p[i]=p[i-1]*(b[i]-i+1llu)%P;
	if(p[n]==0) puts("0"), exit(0);
	for(int i=1; i<=n; ++i) cnt[i]=cnt[i-1]+cmax(u[i]=b[i]-i, 1), q[i]=(u64)q[i-1]*u[i]%P;
	invp[n]=power(p[n], P-2), invq[n]=power(q[n], P-2);
	for(int i=n; i>=1; --i) invp[i-1]=invp[i]*(b[i]-i+1llu)%P;
	for(int i=n; i>=1; --i) invq[i-1]=(u64)invq[i]*u[i]%P;
	u64 invcnt=0;
	for(int i=1; i<=n; ++i) {
		for(int j=a[i]+1; j<=n; j+=j&-j) invcnt+=bit[j];
		for(int j=a[i]; j; j&=j-1) ++bit[j];
	}
	u32 ans=invcnt*2%P;
	memset(bit+1, 0, n*sizeof(int));
	for(int i=1; i<=n; ++i) u[i]=(u64)p[i-1]*invq[i-1]%P*(b[i]-i+1llu)%P;
	for(int i=1; i<=n; ++i) v[i]=(u64)q[i-1]*invp[i]%P;
	for(int i=1, j=0; i<=n; ++i) {
		if(i>1&&b[i-1]==i-1) {
			while(j<i-1) clear(rank[++j]);
		} else {
			ans=(ans+(2llu*query(rank[i]-1)+P-query(n))*v[i])%P;
		}
		add(rank[i], u[i]);
	}
	ans=(ans+(ans&1?P:0llu))/2*p[n]%P;
	printf("%u\n", ans);
	return 0;
}

```