#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back

template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b , 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b , 1 : 0;}

typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;

template<typename I> inline void read(I &x) {
	int f = 0, c;
	while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
	x = c & 15;
	while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
	f ? x = -x : 0;
}

const int N = 2e5 + 7;
const int P = 1e9 + 7;
const int Inv2 = (P + 1) / 2;

#define lowbit(x) ((x) & -(x))

int n, s, ans;
int a[N], b[N], sr[N], ss[N];

inline int smod(int x) { return x >= P ? x - P : x; }
inline void sadd(int &x, const int &y) { x += y; x >= P ? x -= P : x; }
inline int fpow(int x, int y) {
	int ans = 1;
	for (; y; y >>= 1, x = (ll)x * x % P) if (y & 1) ans = (ll)ans * x % P;
	return ans;
}

struct BIT {
	int s[N];
	inline void cls() { memset(s, 0, sizeof(*s) * (n + 1)); }
	inline void qadd(int x, int k) { /*dbg("qadd: x = %d, k = %d\n", x, k);*/ for (; x <= n; x += lowbit(x)) sadd(s[x], k); }
	inline int qsum(int x) {
		int ans = 0;
		for (; x; x -= lowbit(x)) sadd(ans, s[x]);
		return ans;
	}
	inline int qsum(int l, int r) { /*dbg("qsum: l = %d, r = %d\n", l, r);*/ return smod(qsum(r) + P - qsum(l - 1)); }
}sa, sb;

inline void ycl() {
	for (int i = n; i; --i) b[i] += b[i + 1];//, dbg("i = %d, b[i] = %d\n", i, b[i]);
	s = 1;
	for (int i = n; i; --i) { 
		ss[i] = b[i] - (n - i);
		s = (ll)s * ss[i] % P;
		if (i == n || ss[i + 1] == 1) sr[i] = i;
		else sr[i] = sr[i + 1];
		// dbg("i = %d, b[i] = %d, ss[i] = %d, sr[i] = %d\n", i, b[i], ss[i], sr[i]);
	}
	ss[0] = 1;
	for (int i = 1; i <= n; ++i)
		if (ss[i] == 1) ss[i] = ss[i - 1];
		else ss[i] = (ll)ss[i - 1] * (ss[i] - 1) % P * fpow(ss[i], P - 2) % P;
}

inline void work() {
	ycl();
	for (int i = n; i; --i) {
		sadd(ans, (ll)s * sa.qsum(a[i], sr[a[i]]) % P * fpow(ss[a[i]], P - 2) % P);
		// dbg("** i = %d, ans = %d, a[i] = %d, ss[i] = %d, %d %d\n", i, ans, a[i], ss[i], sa.qsum(a[i], sr[a[i]]), fpow(ss[a[i]], P - 2));
		sa.qadd(a[i], ss[a[i]]);
	}
	sa.cls();
	for (int i = 1; i <= n; ++i) {
		sadd(ans, (2ll * s * sb.qsum(a[i] + 1, n) + (ll)(P - s) * sa.qsum(a[i] + 1, sr[a[i]]) % P * fpow(ss[a[i]], P - 2)) % P);
		sa.qadd(a[i], ss[a[i]]), sb.qadd(a[i], 1);
		// dbg("***** i = %d, ans = %d, a[i] = %d, ss[i] = %d, %d, %d %d\n", i, ans, a[i], ss[i], sb.qsum(a[i] + 1, sr[a[i]]), sa.qsum(a[i], sr[a[i]]), fpow(ss[a[i]], P - 2));
	}
	ans = (ll)ans * Inv2 % P;
	printf("%d\n", ans);
}

inline void init() {
	read(n);
	for (int i = 1; i <= n; ++i) read(a[i]), ++b[a[i]];
}

int main() {
#ifdef hzhkk
	freopen("hkk.in", "r", stdin);
#endif
	init();
	work();
	fclose(stdin), fclose(stdout);
	return 0;
}