Problem:
Score : 1700 points

Problem Statement

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.

Notes

The inversion number of a sequence Z whose length N is the number of pairs of integers i and j ( 1 \leq i < j \leq N ) such that Z_i > Z_j.

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.

Sample Input 1

3
2 3 3

Sample Output 1

4

There are four permutations that satisfy the condition: (1,2,3), (1,3,2), (2,1,3) and (2,3,1).
The inversion numbers of these permutations are 0, 1, 1 and 2, respectively, for a total of 4.

Sample Input 2

6
4 2 5 1 6 3

Sample Output 2

7

Only one permutation (4,2,5,1,6,3) satisfies the condition.
The inversion number of this permutation is 7, so the answer is 7.

Sample Input 3

5
4 4 4 4 4

Sample Output 3

0

No permutation satisfies the condition.

Sample Input 4

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

Sample Output 4

848414012

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

/****************************************************************
*	Author: huhao
*	Email: 826538400@qq.com
*	Create time: 2020-02-12 10:42:26
****************************************************************/
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#define fr(i,a,b) for(int i=(a),end_##i=(b);i<=end_##i;i++)
#define fd(i,a,b) for(int i=(a),end_##i=(b);i>=end_##i;i--)
int read()
{
	int r=0,t=1,c=getchar();
	while(c<'0'||c>'9')
	{
		t=c=='-'?-1:1;
		c=getchar();
	}
	while(c>='0'&&c<='9')
	{
		r=(r<<3)+(r<<1)+(c^48);
		c=getchar();
	}
	return r*t;
}
#define i64 long long
const int N=200010,mod=1000000007,i2=(mod+1)/2;
i64 n,s[N],v[N],w[N],zr[N],lst[N],a[N],b1[N],b2[N],ans,Ans;
i64 &Add(i64 &a,i64 b){ return a=(a+b)%mod; }
void add(i64 *b,int p,i64 v)
{
	while(p){ Add(b[p],v); p-=p&(-p); }
}
i64 query(i64 *b,int p)
{
	i64 ans=0;
	while(p<=n){ Add(ans,b[p]); p+=p&(-p); }
	return ans;
}
i64 power(i64 a,i64 b,i64 p)
{
	i64 r=1; a%=p;
	for(;b;b>>=1,a=a*a%p) if(b&1) r=r*a%p;
	return r;
}
i64 inv(i64 a){ return power(a,mod-2,mod); }
i64 Query(i64 *b,int l,int r){ return (query(b,l)-query(b,r+1)+mod)%mod; }
int main()
{
	n=read();
	fr(i,1,n) s[a[i]=read()]++;
	fd(i,n,1) s[i]+=s[i+1];
	Ans=1;
	fr(i,1,n)
	{
		v[i]=s[i]-(n-i); Ans=Ans*v[i]%mod;
		if(!v[i]){ printf("0\n"); return 0; }
		if(v[i]-1){ zr[i]=zr[i-1]; w[i]=w[i-1]*(v[i]-1)%mod*inv(v[i])%mod; }
		else{ zr[i]=zr[i-1]+1; w[i]=1; }
		lst[zr[i]]=i;
	}
	fd(i,n,1)
	{
		Add(ans,Ans*(inv(w[a[i]])*Query(b1,a[i]+1,lst[zr[a[i]]])%mod*i2%mod));
		add(b1,a[i],w[a[i]]);
	}
//	printf("%d\n",int(Ans));
//	fr(i,1,n) printf("%d%c",int(zr[i]),i==n?'\n':' ');
	fr(i,1,n) b1[i]=0;
	fr(i,1,n)
	{
		Add(ans,Ans*(Query(b2,a[i],n)-Query(b1,a[i],lst[zr[a[i]]])*inv(w[a[i]])%mod*i2%mod+mod)%mod);
		add(b1,a[i],w[a[i]]); add(b2,a[i],1);
	}
	printf("%d\n",int(ans));
	return 0;
}