#include <bits/stdc++.h>

typedef long long LL;
const int N = 200005, mod = 1000000007;

void reduce(int &x) { x += x >> 31 & mod; }
int pow(int x, int y, int ans = 1) {
	for (; y; y >>= 1, x = (LL) x * x % mod)
		if (y & 1) ans = (LL) ans * x % mod;
	return ans;
}

int n, a[N], b[N], t[N], it[N], pre[N], v[N], inversion, number;
std::vector<int> id[N];

void solve(std::vector<int> &points) {
	static int bit[N];
	std::sort(points.begin(), points.end());
	for (int p : points) {
		int sum = 0;
		for (int j = a[p] - 1; j; j &= j - 1) reduce(sum += bit[j] - mod);
		inversion = (inversion + (LL) sum * t[a[p]]) % mod;
		for (int j = a[p]; j <= n; j += j & -j) reduce(bit[j] += it[a[p]] - mod);
	}
	for (int p : points) for (int j = a[p]; j <= n; j += j & -j) bit[j] = 0;
	for (int p : points) {
		int sum = 0;
		for (int j = a[p] + 1; j <= n; j += j & -j) reduce(sum += bit[j] - mod);
		inversion = (inversion + (LL) (mod - sum) * it[a[p]]) % mod;
		for (int j = a[p]; j; j &= j - 1) reduce(bit[j] += t[a[p]] - mod);
	}
	for (int p : points) for (int j = a[p]; j; j &= j - 1) bit[j] = 0;
	points.clear();
}

int main() {
	std::ios::sync_with_stdio(0), std::cin.tie(0);
	std::cin >> n;
	for (int i = 1; i <= n; ++i) std::cin >> a[i], ++b[a[i]], id[a[i]].emplace_back(i);

	{
		static int bit[N];
		for (int i = 1; i <= n; ++i) {
			for (int j = a[i]; j; j &= j - 1) ++bit[j];
			for (int j = a[i] + 1; j <= n; j += j & -j) reduce(inversion += bit[j] - mod);
		}
		inversion = inversion * 2LL % mod;
		for (int i = 1; i <= n; ++i)
			inversion = (inversion + (LL) b[i] * (b[i] - 1) / 2) % mod;
	}

	for (int i = 1; i <= n; ++i) v[i] = a[i]; std::sort(v + 1, v + n + 1);
	number = 1; for (int i = 1; i <= n; ++i) number = (LL) number * (v[i] - i + 1) % mod;
	if (!number) return std::cout << "0\n", 0;
	number = (LL) number * (mod + 1 >> 1) % mod;

	
	for (int i = n - 1; i; --i) b[i] += b[i + 1];
	for (int i = 1; i <= n; ++i) b[i] -= n - i;

	for (int i = 1; i <= n; ++i) assert(b[i] > 0);

	for (int i = 1; i <= n; ++i) t[i] = pow(b[i], mod - 2, b[i] - 1), it[i] = pow(b[i] - 1, mod - 2, b[i]);

	std::vector<int> now;
	for (int i = 0; i <= n + 1; ++i) {
		if (!t[i]) {
			solve(now), now.insert(now.end(), id[i].begin(), id[i].end());
			t[i] = it[i] = 1;
		} else {
			now.insert(now.end(), id[i].begin(), id[i].end());
			t[i] = (LL) t[i] * t[i - 1] % mod;
			it[i] = (LL) it[i] * it[i - 1] % mod;
		}
	}

	std::cout << (LL) inversion * number % mod << '\n';
	return 0;
}