#include <iostream>

using i64 = long long;

const i64 MOD = 1000000007;
const int N = 200000 + 7;

int n;
int a[N], c[N], e[N];
int cril, crir;
i64 f[N], g[N], h[N], q[N];
i64 ans, s = 1;

i64 pow(i64 a, i64 k) {
	i64 t = 1;
	for (; k; a = a * a % MOD, k >>= 1) if (k & 1) t = t * a % MOD;
	return t;
}

i64 bit[N];
void add(int i, i64 v) {
	for (; i <= n; i += i & -i) ( bit[i] += v ) %= MOD;
}
i64 sum(int i) {
	i64 t = 0;
	for (; i; i -= i & -i) t += bit[i];
	return t % MOD;
}

int main() {
//	freopen("code.in", "r", stdin);
//	freopen("code.out", "w", stdout);

	scanf("%d", &n);
	for (int i = 1; i <= n; ++i)
		scanf("%d", a + i), ++c[a[i]];
	for (int i = n; i; --i) {
		c[i] += c[i + 1];
		if (c[i] - n + i - 1 <= 0) cril = i, crir = crir ? crir : i;
		f[i] = (c[i] - n + i - 1) * pow(c[i] - n + i, MOD - 2) % MOD;
		s = s * std::max(c[i] - n + i, 0) % MOD;
	}
	for (int i = g[0] = h[0] = 1; i <= n; ++i)
		g[i] = g[i - 1] * (f[i] ? f[i] : 1) % MOD,
		h[i] = h[i - 1] * pow(f[i] ? f[i] : 1, MOD - 2) % MOD,
		q[i] = f[i] ? q[i - 1] : i;

	for (int i = 1; i <= n; ++i) e[c[a[i]]--] = i;
	for (int i = 1, j; i <= n; i = j) {
		if (!f[a[e[i]]]) { j = i + 1; continue; }
		for (j = i; j <= n && a[e[j]] > q[a[e[i]]]; ++j)
			( ans += (sum(n) - sum(e[j]) - sum(e[j] - 1)) * h[a[e[j]]] ) %= MOD,
			add(e[j], g[a[e[j]]]);
		for (j = i; j <= n && a[e[j]] > q[a[e[i]]]; ++j)
			add(e[j], -g[a[e[j]]]);
	}
	for (int i = 1; i <= n; ++i)
		ans += sum(e[i] - 1), add(e[i], 2);
	ans = (ans % MOD * s % MOD * (-MOD / 2) % MOD + MOD) % MOD;
	printf("%lld\n", ans);
	return 0;
}
