#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;
}


Measured memory_bytes: 15360