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 <algorithm>  
#include <iostream>  
#include <sstream>  
#include <string>  
#include <cstring>
#include <vector>  
#include <queue>  
#include <set>  
#include <map>  
#include <cstdio>  
#include <cstdlib>  
#include <cctype>  
#include <cmath>  
#include <list>  
#include <cassert>
#include <ctime>
#include <climits>
using namespace std;  

#define PB push_back  
#define MP make_pair  
#define SZ(v) ((int)(v).size())  
#define FOR(i,a,b) for(int i=(a);i<(b);++i)  
#define REP(i,n) FOR(i,0,n)  
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)  
#define REPE(i,n) FORE(i,0,n)  
#define FORSZ(i,a,v) FOR(i,a,SZ(v))  
#define REPSZ(i,v) REP(i,SZ(v))  
typedef long long ll;
typedef unsigned long long ull;
ll gcd(ll a,ll b) { return b==0?a:gcd(b,a%b); }

const int MAXN = 200000;
const int MOD = 1000000007;
const int INV2 = (MOD + 1) / 2;

int n;
int a[MAXN];

int b[MAXN + 1];

int calc() {
	FORE(i, 1, n) b[i] = 0; REP(i, n) b[a[i]]++; for (int i = n - 1; i >= 1; --i) b[i] += b[i + 1];
	int ret = 1; FORE(i, 1, n) ret = (ll)ret*(b[i] - (n - i)) % MOD;
	//printf("a:"); REP(i, n) printf(" %d", a[i]); printf(" -> %d\n", ret);
	return ret;
}

int solve1() {
	int ret = 0;
	int sperms = calc();
	REP(i, n) FOR(j, i + 1, n) {
		if (a[i] <= a[j]) {
			int tmp = a[j]; a[j] = a[i];
			ret = (ret + (ll)INV2*calc()) % MOD;
			a[j] = tmp;
		} else {
			int tmp = a[i]; a[i] = a[j];
			ret = ((ll)ret + sperms + MOD - (ll)INV2*calc() % MOD) % MOD;
			a[i] = tmp;
		}
	}
	return ret;
}

int solve2() {
	int ret = 0;
	int sperms = calc();
	REP(i, n) REP(j, n) if (a[i] > a[j]) {
		int tmp = a[i]; a[i] = a[j];
		int cur = (ll)INV2*calc() % MOD;
		if (i > j) ret = (ret + cur) % MOD; else ret = ((ll)ret + sperms + MOD - cur) % MOD;
		a[i] = tmp;
	}
	REP(i, n) FOR(j, i + 1, n) if (a[i] == a[j]) ret = (ret + (ll)INV2*sperms) % MOD;
	return ret;
}

pair<int, int> c[MAXN];
int d[MAXN + 1];
int e[MAXN + 1];

int solve3() {
	int ret = 0;
	REP(i, n) c[i] = MP(a[i], i); sort(c, c + n);
	FORE(i, 1, n) d[i] = 0; REP(i, n) ++d[a[i]]; for (int i = n - 1; i >= 1; --i) d[i] += d[i + 1];
	int sperms = 1; FORE(i, 1, n) sperms = (ll)sperms*(d[i] - (n - i)) % MOD;
	REP(i, n) FOR(j, i + 1, n) if (c[i].first < c[j].first) {
		FORE(k, 1, n) e[k] = d[k];
		FORE(k, c[i].first + 1, c[j].first) --e[k];
		int now = 1; FORE(k, 1, n) now = (ll)now*(e[k] - (n - k)) % MOD;
		int cur = (ll)INV2*now%MOD;
		if (c[i].second < c[j].second) ret = (ret + cur) % MOD; else ret = ((ll)ret + sperms + MOD - cur) % MOD;
	}
	REP(i, n) FOR(j, i + 1, n) if (c[i].first == c[j].first) ret = (ret + (ll)INV2*sperms) % MOD;
	return ret;
}

struct V {
	int prod, nzero;
	V() :prod(1), nzero(0) {}
	V(int x) { if (x == 0) prod = 1, nzero = 1; else prod = x, nzero = 0; }
	V(int prod, int nzero) :prod(prod), nzero(nzero) {}
	int val() { return nzero == 0 ? prod : 0; }
	void print() { printf("%d", prod); if (nzero != 0) printf("[%d]", nzero); }
};
V operator*(const V &a, const V &b) { return V((ll)a.prod*b.prod%MOD, a.nzero + b.nzero); }

int inv[MAXN + 1];
V f[MAXN + 1];
V fprod[MAXN + 1];
V finv[MAXN + 1];
V finvprod[MAXN + 1];

int solve4() {
	int ret = 0;
	REP(i, n) c[i] = MP(a[i], i); sort(c, c + n);
	FORE(i, 1, n) d[i] = 0; REP(i, n) ++d[a[i]]; for (int i = n - 1; i >= 1; --i) d[i] += d[i + 1];
	int sperms = 1; FORE(i, 1, n) sperms = (ll)sperms*(d[i] - (n - i)) % MOD; if (sperms == 0) return 0;
	
	inv[1] = 1; FORE(i, 2, n) inv[i] = (ll)(MOD - MOD / i)*inv[MOD%i] % MOD;
	FORE(i, 1, n) f[i] = V(d[i] - 1 - (n - i))*V(inv[d[i] - (n - i)]);
	FORE(i, 1, n) finv[i] = V(d[i] - (n - i))*(d[i] - 1 - (n - i) == 0 ? V(1, -1) : V(inv[d[i] - 1 - (n - i)]));
	fprod[0] = V(1); FORE(i, 1, n) fprod[i] = fprod[i - 1] * f[i];
	finvprod[0] = V(1); FORE(i, 1, n) finvprod[i] = finvprod[i - 1] * finv[i];

	//printf("d:"); FORE(i, 1, n) printf(" %d", d[i]); puts("");
	//printf("f:"); FORE(i, 1, n) { printf(" "); f[i].print(); } puts("");
	//printf("finv:"); FORE(i, 1, n) { printf(" "); finv[i].print(); } puts("");

	REP(i, n) FOR(j, i + 1, n) if (c[i].first < c[j].first) {
		V now = V(sperms) * fprod[c[j].first] * finvprod[c[i].first];
		//printf("%d-%d: %d %c\n", i, j, now.val(), c[i].second < c[j].second ? '+' : '-');
		int cur = (ll)INV2*now.val() % MOD;
		if (c[i].second < c[j].second) ret = (ret + cur) % MOD; else ret = ((ll)ret + sperms + MOD - cur) % MOD;
	}
	REP(i, n) FOR(j, i + 1, n) if (c[i].first == c[j].first) ret = (ret + (ll)INV2*sperms) % MOD;
	return ret;
}

int pos[MAXN];
int l[MAXN], r[MAXN];

int bit[MAXN + 2], bcnt[MAXN + 2];
void bmod(int idx, int by) { ++idx;  while (idx <= n + 1) { ++bcnt[idx], bit[idx] = (bit[idx] + by) % MOD; idx += idx&-idx; } }
pair<int, int> bget(int idx) { ++idx; pair<int, int> ret = MP(0, 0); while (idx > 0) { ret = MP(ret.first + bcnt[idx], (ret.second + bit[idx]) % MOD); idx -= idx&-idx; } return ret; }
pair<int, int> bget(int lidx, int ridx) { pair<int, int> a = bget(ridx - 1), b = bget(lidx - 1); return MP(a.first - b.first, (a.second + MOD - b.second) % MOD); }

int solve5() {
	int ret = 0;
	REP(i, n) c[i] = MP(a[i], i); sort(c, c + n);
	FORE(i, 1, n) d[i] = 0; REP(i, n) ++d[a[i]]; for (int i = n - 1; i >= 1; --i) d[i] += d[i + 1];
	int sperms = 1; FORE(i, 1, n) sperms = (ll)sperms*(d[i] - (n - i)) % MOD; if (sperms == 0) return 0;

	inv[1] = 1; FORE(i, 2, n) inv[i] = (ll)(MOD - MOD / i)*inv[MOD%i] % MOD;
	FORE(i, 1, n) f[i] = V(d[i] - 1 - (n - i))*V(inv[d[i] - (n - i)]);
	FORE(i, 1, n) finv[i] = V(d[i] - (n - i))*(d[i] - 1 - (n - i) == 0 ? V(1, -1) : V(inv[d[i] - 1 - (n - i)]));
	fprod[0] = V(1); FORE(i, 1, n) fprod[i] = fprod[i - 1] * f[i];
	finvprod[0] = V(1); FORE(i, 1, n) finvprod[i] = finvprod[i - 1] * finv[i];

	//printf("d:"); FORE(i, 1, n) printf(" %d", d[i]); puts("");
	//printf("f:"); FORE(i, 1, n) { printf(" "); f[i].print(); } puts("");
	//printf("finv:"); FORE(i, 1, n) { printf(" "); finv[i].print(); } puts("");

	REP(i, n) pos[c[i].second] = i;
	REP(j,n) {
		r[j] = c[j].first;
		l[j] = j == 0 ? 1 : l[j - 1]; while (fprod[l[j]].nzero != fprod[c[j].first].nzero) ++l[j];
	}

	REPE(i, n + 1) bcnt[i] = bit[i] = 0;
	REP(idx, n) {
		int j = pos[idx];
		// we want sum(finvprod[c[i].first]) for c[i].first<c[j].first, c[i].second<c[j].second, nzero[c[i].first]=nzero[c[j].first]
		// which means bget [l..r)
		pair<int, int> now = bget(l[j], r[j]);
		int cur = (ll)INV2*sperms%MOD*fprod[c[j].first].prod%MOD*now.second%MOD;
		//printf("+ j=%d now=%d/%d [%d..%d) cur=%d -> %d by %d\n", j, now.first, now.second, l[j], r[j], cur, c[j].first, finvprod[c[j].first].prod);
		ret = (ret + cur) % MOD;
		bmod(c[j].first, finvprod[c[j].first].prod);
	}
	REPE(i, n + 1) bcnt[i] = bit[i] = 0;
	for (int idx = n - 1; idx >= 0; --idx) {
		int j = pos[idx];
		// we want sum(finvprod[c[i].first]) for c[i].first<c[j].first, c[i].second>c[j].second, nzero[c[i].first]=nzero[c[j].first]
		// which means bget [l..r)
		pair<int, int> now = bget(l[j], r[j]); pair<int, int> now2 = bget(1, r[j]);
		int cur = ((ll)sperms*now2.first + MOD - (ll)INV2*sperms%MOD*fprod[c[j].first].prod % MOD*now.second%MOD) % MOD;
		//printf("- j=%d now=%d/%d [%d..%d) cur=%d -> %d by %d\n", j, now.first, now.second, l[j], r[j], cur, c[j].first, finvprod[c[j].first].prod);
		ret = (ret + cur) % MOD;
		bmod(c[j].first, finvprod[c[j].first].prod);
	}
	for (int l = 0, r = l; l < n; l = r) {
		while (r < n&&c[l].first == c[r].first) ++r;
		int cnt = r - l, npairs = (ll)cnt*(cnt - 1) / 2 % MOD;
		ret = (ret + (ll)INV2*sperms%MOD*npairs) % MOD;
	}
	return ret;
}

void run() {
	scanf("%d", &n); REP(i, n) scanf("%d", &a[i]);

	//printf("%d\n", solve1());
	//printf("%d\n", solve2());
	//printf("%d\n", solve3());
	//printf("%d\n", solve4());
	printf("%d\n", solve5());
}

int main() {
	run();
	return 0;
}