#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
const int P = 1e9 + 7;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); } 
template <typename T> void read(T &x) {
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
template <typename T> void write(T x) {
	if (x < 0) x = -x, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
void update(int &x, int y) {
	x += y;
	if (x >= P) x -= P;
}
struct BinaryIndexTree {
	int n, a[MAXN];
	void init(int x) {
		n = x;
		memset(a, 0, sizeof(a));
	}
	void modify(int x, int d) {
		for (int i = x; i <= n; i += i & -i)
			update(a[i], d);
	}
	int query(int l, int r) {
		int ans = 0;
		for (int i = r; i >= 1; i -= i & -i)
			update(ans, a[i]);
		for (int i = l - 1; i >= 1; i -= i & -i)
			update(ans, P - a[i]);
		return ans;
	}
} BIT, CIT;
int power(int x, int y) {
	if (y == 0) return 1;
	int tmp = power(x, y / 2);
	if (y % 2 == 0) return 1ll * tmp * tmp % P;
	else return 1ll * tmp * tmp % P * x % P;
}
int n, s, a[MAXN], cnt[MAXN];
int Max[MAXN], pre[MAXN], val[MAXN];
int main() {
	read(n), s = 1;
	for (int i = 1; i <= n; i++)
		read(a[i]), cnt[a[i]]++;
	for (int i = n; i >= 1; i--)
		cnt[i] += cnt[i + 1];
	for (int i = 1; i <= n; i++) {
		cnt[i] -= n - i;
		s = 1ll * s * cnt[i] % P;
	}
	if (s == 0) {
		puts("0");
		return 0;
	}
	for (int i = 1; i <= n; i++)
		val[i] = (cnt[i] - 1ll) * power(cnt[i], P - 2) % P;
	int last = 0; pre[0] = 1;
	for (int i = 1; i <= n; i++) {
		if (val[i] == 0) {
			for (int j = last; j <= i - 1; j++)
				Max[j] = i - 1;
			last = i, pre[i] = 1;
		} else pre[i] = 1ll * pre[i - 1] * val[i] % P;
	}
	for (int i = last; i <= n; i++)
		Max[i] = n;
	int ans = 0; BIT.init(n);
	for (int i = n; i >= 1; i--) {
		update(ans, 1ll * s * power(2ll * pre[a[i]] % P, P - 2) % P * BIT.query(a[i], Max[a[i]]) % P);
		BIT.modify(a[i], pre[a[i]]);
	}
	BIT.init(n), CIT.init(n);
	for (int i = 1; i <= n; i++) {
		update(ans, P - 1ll * s * power(2ll * pre[a[i]] % P, P - 2) % P * BIT.query(a[i] + 1, Max[a[i]]) % P);
		update(ans, 1ll * s * CIT.query(a[i] + 1, n) % P);
		BIT.modify(a[i], pre[a[i]]);
		CIT.modify(a[i], 1);
	}
	writeln(ans); 
	return 0;
}