#include <bits/stdc++.h>
#define EB emplace_back

typedef long long ll;
typedef std::vector <int> vector;
const int N = 200054, mod = 1000000007, iv2 = (mod + 1) / 2;

int n;
int a[N], b[N], inv[N];
int cnt = 0, seg[N], col[N];
int f[N], g[N];
ll ans = 0;
vector U[N];

inline void add(int &x, const int y) {x += y - mod, x += x >> 31 & mod;}
inline void sub(int &x, const int y) {x -= y, x += x >> 31 & mod;}
ll PowerMod(ll a, int n, ll c = 1) {for (; n; n >>= 1, a = a * a % mod) if (n & 1) c = c * a % mod; return c;}

namespace BIT {
	#define lowbit(x) (x & -x)
	int n, ti, tag[N], x[N];
	inline void resize(int size) {n = size, ++ti;}
	inline void clear() {++ti;}
	inline int & recover(int id) {return tag[id] == ti ? x[id] : (tag[id] = ti, x[id] = 0);}
	int sum(int h) {int s = 0; for (; h > 0; h -= lowbit(h)) ::add(s, recover(h)); return s;}
	void add(int h, int v) {for (; h <= n; h += lowbit(h)) ::add(recover(h), v);}
}

int solve(int id) {
	int i, l = seg[id - 1], r = seg[id] - 1, ret = 0;
	BIT::resize(r - l + 1);
	for (f[l] = g[l] = 1, i = l; i <= r; ++i)
		f[i + 1] = f[i] * (b[i] - 1ll) % mod * inv[b[i]] % mod, g[i + 1] = PowerMod(f[i + 1], mod - 2);
	for (int x : U[id]) ret = (ret + (ll)g[x] * BIT::sum(r - x + 1)) % mod, BIT::add(r - x + 1, f[x]);
	BIT::clear();
	for (int x : U[id]) ret = (ret - (ll)f[x] * BIT::sum(x - l)) % mod, BIT::add(x - l + 1, g[x]);
	return ret + (ret >> 31 & mod);
}

void merge_sort(int L, int R) {
	if (L + 1 == R) return;
	int M = (L + R) / 2, i = L, j = M, c = L;
	static int fy[N];
	merge_sort(L, M), merge_sort(M, R), memcpy(fy + L, a + L, (R - L) << 2);
	for (; i < M && j < R; a[c++] = (fy[i] >= fy[j] ? fy[i++] : (ans += 2 * (M - i), fy[j++])));
	i < M ? memcpy(a + c, fy + i, (M - i) << 2) : memcpy(a + c, fy + j, (R - j) << 2);
}

int main() {
	int i, initial = 1;
	scanf("%d", &n);
	for (inv[1] = 1, i = 2; i <= n; ++i) inv[i] = ll(mod - mod / i) * inv[mod % i] % mod;
	for (i = 0; i < n; ++i) scanf("%d", a + i), a[i] = n - a[i], ++b[a[i]];
	for (i = 1; i < n; ++i) b[i] += b[i - 1] - 1;
	for (i = 0; i < n; ++i) initial = (ll)initial * b[i] % mod;
	if (!initial) return putchar(48), putchar(10), 0;
	seg[cnt++] = 0;
	for (i = 0; i < n; ++i) if (b[i] == 1) seg[cnt++] = i + 1;
	for (i = 1; i < cnt; ++i) std::fill(col + seg[i - 1], col + seg[i], i);
	for (i = 0; i < n; ++i) U[col[a[i]]].EB(a[i]);
	for (i = 1; i < cnt; ++i) ans += solve(i);
	merge_sort(0, n);
	ans = ans % mod * initial % mod * iv2 % mod;
	printf("%d\n", (int)ans);
	return 0;
}