#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7, inv2 = (mod + 1) / 2;
const int N = 200010;
int n, a[N], cnt[N], tot = 1, ans;
int tr[2][N], up[N], dn[N], zr[N], idn[N];

inline int add(const int &x, const int &y) {
	return x + y < mod ? x + y : x + y - mod;
}
inline int sub(const int &x, const int &y) {
	return x - y < 0 ? x - y + mod : x - y;
}
inline int mul(const int &x, const int &y) { return (int)((ll)x * y % mod); }
int ksm(int x, int y = mod - 2) {
	int ss = 1; for(; y; y >>= 1, x = mul(x, x)) if(y & 1) ss = mul(ss, x);
	return ss;
}
void Add(int x, int w) {
	for(; x <= n; x += x & -x)
		tr[0][x] = add(tr[0][x], w), tr[1][x] = add(tr[1][x], 1);
}
int Ask(int x, int w) {
	int ss = 0; for(; x; x -= x & -x) ss = add(ss, tr[w][x]); return ss;
}
int main() {
	scanf("%d", &n);
	for(int i = 1; i <= n; i++) scanf("%d", &a[i]), ++cnt[a[i]];
	for(int i = n; i; i--) cnt[i] += cnt[i + 1];
	for(int i = n; i; i--)
		tot = mul(tot, sub(cnt[i], n - i)), cnt[i] -= n - i;
	if(!tot) { puts("0"); return 0; }
	up[0] = dn[0] = 1;
	for(int i = 1, x; i <= n; i++) {
		x = mul(cnt[i] - 1, ksm(cnt[i]));
		if(!x) zr[i] = zr[i - 1] + 1, up[zr[i]] = i, dn[i] = dn[i - 1];
		else zr[i] = zr[i - 1], dn[i] = mul(dn[i - 1], x);
		idn[i] = ksm(dn[i]);
	}
	for(int i = 1, x; i <= n; i++) {
		x = sub(Ask(a[i], 0), Ask(up[zr[a[i]]] - 1, 0));
		x = mul(x, mul(dn[a[i]], mul(tot, inv2)));
		ans = add(ans, x);
		Add(a[i], idn[a[i]]);
	}
	for(int i = 1; i <= n; i++) tr[0][i] = tr[1][i] = 0;
	for(int i = n, x; i; i--) {
		x = sub(Ask(a[i] - 1, 0), Ask(up[zr[a[i]]] - 1, 0));
		x = mul(x, mul(dn[a[i]], mul(tot, inv2)));
		ans = sub(ans, x);
		ans = add(ans, mul(Ask(a[i] - 1, 1), tot));
		Add(a[i], idn[a[i]]);
	}
	printf("%d\n", ans);
	return 0;
}