#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=1000000007;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}

class range {private: struct I { int x; int operator*() { return x; }bool operator!=(I& lhs) { return x<lhs.x; }void operator++() { ++x; } }; I i, n;public:range(int n) :i({ 0 }), n({ n }) {}range(int i, int n) :i({ i }), n({ n }) {}I& begin() { return i; }I& end() { return n; }};

// head

const int N=401000;
int n,vis[N];
PII p[N];
ll ret,prec[N],sufc[N],c[N];

struct node {
	ll fgp,fgv;
	ll sp,sv;
}nd[4*N];
void upd(int p) {
	nd[p].sp=(nd[p+p].sp+nd[p+p+1].sp)%mod;
	nd[p].sv=(nd[p+p].sv+nd[p+p+1].sv)%mod;
}
void setf(int p,ll fgp,ll fgv) {
	nd[p].fgv=(nd[p].fgv+fgv*nd[p].fgp)%mod;
	nd[p].fgp=nd[p].fgp*fgp%mod;
	nd[p].sv=(nd[p].sv+fgv*nd[p].sp)%mod;
	nd[p].sp=nd[p].sp*fgp%mod;
}
void build(int p,int l,int r) {
	nd[p].fgp=1;
	if (l==r) {
	} else {
		int md=(l+r)>>1;
		build(p+p,l,md);
		build(p+p+1,md+1,r);
		upd(p);
	}
}
void push(int p) {
	if (nd[p].fgp!=1||nd[p].fgv!=0) {
		setf(p+p,nd[p].fgp,nd[p].fgv);
		setf(p+p+1,nd[p].fgp,nd[p].fgv);
		nd[p].fgp=1;
		nd[p].fgv=0;
	}
}
ll query(int p,int l,int r,int tl,int tr) {
	if (tl==l&&tr==r) return nd[p].sv;
	else {
		push(p);
		int md=(l+r)>>1;
		if (tr<=md) return query(p+p,l,md,tl,tr);
		else if (tl>md) return query(p+p+1,md+1,r,tl,tr);
		else return (query(p+p,l,md,tl,md)+query(p+p+1,md+1,r,md+1,tr))%mod;
	}
}
void modify(int p,int l,int r,int x,ll v) {
	if (l==r) {
		nd[p].sp=v;
	} else {
		push(p);
		int md=(l+r)>>1;
		if (x<=md) modify(p+p,l,md,x,v);
		else if (x>md) modify(p+p+1,md+1,r,x,v);
		upd(p);
	}
}

int main() {
	scanf("%d",&n);
	rep(i,1,n+1) scanf("%d",&p[i].fi),p[i].se=i;
	sort(p+1,p+n+1);
	rep(i,1,n+1) c[i]=p[i].fi-i+1;
	prec[0]=1;
	sufc[n]=1;
	rep(i,1,n+1) prec[i]=prec[i-1]*c[i]%mod;
	per(i,1,n+1) sufc[i-1]=sufc[i]*c[i]%mod;
	ll totval=0;
	build(1,1,n);
	per(i,1,n+1) {
		totval=(totval*(c[i+1]-1)+sufc[i+1])%mod;
		ret=(ret+totval*prec[i-1]%mod*(c[i]*(c[i]-1)/2%mod))%mod;
		ll key=prec[i]*(p[i+1].fi-p[i].fi)%mod;

		setf(1,c[i+1]-1,0);
		if (i<n) modify(1,1,n,p[i+1].se,sufc[i+1]);
		setf(1,1,key);
		if (p[i].se>1) ret=(ret+query(1,1,n,1,p[i].se-1))%mod;
	}
	printf("%lld\n",ret);
}

### 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
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=1000000007;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}

class range {private: struct I { int x; int operator*() { return x; }bool operator!=(I& lhs) { return x<lhs.x; }void operator++() { ++x; } }; I i, n;public:range(int n) :i({ 0 }), n({ n }) {}range(int i, int n) :i({ i }), n({ n }) {}I& begin() { return i; }I& end() { return n; }};

// head

const int N=401000;
int n,vis[N];
PII p[N];
ll ret,prec[N],sufc[N],c[N];

struct node {
	ll fgp,fgv;
	ll sp,sv;
}nd[4*N];
void upd(int p) {
	nd[p].sp=(nd[p+p].sp+nd[p+p+1].sp)%mod;
	nd[p].sv=(nd[p+p].sv+nd[p+p+1].sv)%mod;
}
void setf(int p,ll fgp,ll fgv) {
	nd[p].fgv=(nd[p].fgv+fgv*nd[p].fgp)%mod;
	nd[p].fgp=nd[p].fgp*fgp%mod;
	nd[p].sv=(nd[p].sv+fgv*nd[p].sp)%mod;
	nd[p].sp=nd[p].sp*fgp%mod;
}
void build(int p,int l,int r) {
	nd[p].fgp=1;
	if (l==r) {
	} else {
		int md=(l+r)>>1;
		build(p+p,l,md);
		build(p+p+1,md+1,r);
		upd(p);
	}
}
void push(int p) {
	if (nd[p].fgp!=1||nd[p].fgv!=0) {
		setf(p+p,nd[p].fgp,nd[p].fgv);
		setf(p+p+1,nd[p].fgp,nd[p].fgv);
		nd[p].fgp=1;
		nd[p].fgv=0;
	}
}
ll query(int p,int l,int r,int tl,int tr) {
	if (tl==l&&tr==r) return nd[p].sv;
	else {
		push(p);
		int md=(l+r)>>1;
		if (tr<=md) return query(p+p,l,md,tl,tr);
		else if (tl>md) return query(p+p+1,md+1,r,tl,tr);
		else return (query(p+p,l,md,tl,md)+query(p+p+1,md+1,r,md+1,tr))%mod;
	}
}
void modify(int p,int l,int r,int x,ll v) {
	if (l==r) {
		nd[p].sp=v;
	} else {
		push(p);
		int md=(l+r)>>1;
		if (x<=md) modify(p+p,l,md,x,v);
		else if (x>md) modify(p+p+1,md+1,r,x,v);
		upd(p);
	}
}

int main() {
	scanf("%d",&n);
	rep(i,1,n+1) scanf("%d",&p[i].fi),p[i].se=i;
	sort(p+1,p+n+1);
	rep(i,1,n+1) c[i]=p[i].fi-i+1;
	prec[0]=1;
	sufc[n]=1;
	rep(i,1,n+1) prec[i]=prec[i-1]*c[i]%mod;
	per(i,1,n+1) sufc[i-1]=sufc[i]*c[i]%mod;
	ll totval=0;
	build(1,1,n);
	per(i,1,n+1) {
		totval=(totval*(c[i+1]-1)+sufc[i+1])%mod;
		ret=(ret+totval*prec[i-1]%mod*(c[i]*(c[i]-1)/2%mod))%mod;
		ll key=prec[i]*(p[i+1].fi-p[i].fi)%mod;

		setf(1,c[i+1]-1,0);
		if (i<n) modify(1,1,n,p[i+1].se,sufc[i+1]);
		setf(1,1,key);
		if (p[i].se>1) ret=(ret+query(1,1,n,1,p[i].se-1))%mod;
	}
	printf("%lld\n",ret);
}

```