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)

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#define db double
#define LL long long
#define inf 0x3f3f3f3f
#define N 200005
using namespace std;
int gi()
{
    int res=0,s=1; char ch;
    for(ch=getchar();(ch<'0'||ch>'9')&&ch!='-';ch=getchar());
    if(ch=='-') s=-1,ch=getchar();
    for(;ch>='0'&&ch<='9';ch=getchar()) res=res*10+ch-48;
    return res*s;
}

const int mo=1000000007;
int n,A[N],last[N],tr[N],cnt[N],D[N],mul[N],S=1,ans,sum;

LL qp(LL x,LL y) {
    LL res=1;
    for(;y;y>>=1,x=x*x%mo)
	if(y&1) res=res*x%mo;
    return res;
}
//BIT
int lb(int x) {return x&-x;}
void add(int x,int v) {
    for(;x<=n;x+=lb(x))
	tr[x]+=v,tr[x]>=mo?tr[x]-=mo:0;
}
int ask(int x) {
    int res=0;
    for(;x;x-=lb(x))
	res+=tr[x],res>=mo?res-=mo:0;
    return res;
}
int main()
{
    n=gi();
    for(int i=1;i<=n;++i) A[i]=gi();
    //PRE
    for(int i=1;i<=n;++i) add(A[i],1);
    for(int i=1;i<=n;++i) {
	cnt[i]=(ask(n)-ask(i-1))-(n-i),S=1ll*S*(cnt[i]<=0 ? 0 : cnt[i])%mo;
	if(cnt[i]>0) D[i]=1ll*(cnt[i]-1)*qp(cnt[i],mo-2)%mo;
	if(D[i]) last[i]=last[i-1],mul[i]=1ll*(D[i-1] ? mul[i-1] : 1)*D[i]%mo;
	else last[i]=i,mul[i]=0;
    }
    if(!S) {printf("0\n"); return 0;}
    //PART1
    memset(tr,0,sizeof(tr));
    for(int i=1;i<=n;++i) {
	ans+=1ll*(ask(A[i])-ask(last[A[i]])+mo)%mo*mul[A[i]]%mo*S%mo,ans>=mo?ans-=mo:0;
	add(A[i],qp(mul[A[i]],mo-2));
    }
    ans=1ll*ans*qp(2,mo-2)%mo;
    //PART2
    memset(tr,0,sizeof(tr));
    for(int i=1;i<=n;++i) {
	ans+=1ll*(i-1-ask(A[i]))*S%mo,ans>=mo?ans-=mo:0;
	add(A[i],1);
    }
    memset(tr,0,sizeof(tr));
    for(int i=n;i;--i) {
	if(last[A[i]]==A[i]) continue;
	sum+=1ll*(ask(A[i]-1)-ask(last[A[i]])+mo)%mo*mul[A[i]]%mo*S%mo,sum>=mo?sum-=mo:0;
	add(A[i],qp(mul[A[i]],mo-2));
    }
    sum=1ll*sum*qp(2,mo-2)%mo;
    ans-=sum,ans<0?ans+=mo:0;
    printf("%d\n",ans);
    return 0;
}