#include <bits/stdc++.h>
using namespace std;
typedef bool boolean;

#define ll long long

void exgcd(int a, int b, int& x, int& y) {
	if (!b) {
		x = 1, y = 0;
	} else {
		exgcd(b, a % b, y, x);
		y -= (a / b) * x;
	}
}

int inv(int a, int n) {
	int x, y;
	exgcd(a, n, x, y);
	return (x < 0) ? (x + n) : (x);
}

const int Mod = 1e9 + 7;

template <const int Mod = :: Mod>
class Z {
	public:
		int v;

		Z() : v(0) {	}
		Z(int x) : v(x){
			assert(x >= 0 && x < Mod);
		}
		Z(ll x) : v(x % Mod) {
			assert(v >= 0);
		}

		Z operator + (Z b) {
			int x;
			return Z(((x = v + b.v) >= Mod) ? (x - Mod) : (x));
		}
		Z operator - (Z b) {
			int x;
			return Z(((x = v - b.v) < 0) ? (x + Mod) : (x));
		}
		Z operator * (Z b) {
			return Z(v * 1ll * b.v);
		}
		Z operator ~() {
			return inv(v, Mod);
		}
		Z operator - () {
			return Z(0) - *this;
		}
		Z& operator += (Z b) {
			return *this = *this + b;
		}
		Z& operator -= (Z b) {
			return *this = *this - b;
		}
		Z& operator *= (Z b) {
			return *this = *this * b;
		}
};

typedef Z<> Zi;

const int N = 2e5 + 5;

#define pii pair<int, int>

typedef class FenwickTree {
	public:
		int s;
		Zi* a;
		vector<pii> op;

		FenwickTree() {	}
		FenwickTree(int n) : s(n) {
			a = new Zi[(n + 1)];
			memset(a, 0, sizeof(Zi) * (n + 1));
		}

		void add(int idx, Zi v, boolean clr = false) {
			if (!clr) {
				op.push_back(pii(idx, v.v));
			}
			for ( ; idx <= s; idx += (idx & (-idx)))
				a[idx] += v;
		}
		Zi query(int idx) {
			Zi rt = 0;
			for ( ; idx; idx -= (idx & (-idx)))
				rt += a[idx];
			return rt;
		}
		void clear() {
			for (auto& par : op) {
				add(par.first, -Zi(par.second), true);
			}
			op.clear();
		}
} FenwickTree;

const Zi inv2 = (Mod + 1) >> 1;

int n;
pii _a[N];
Zi P[N], F;
int a[N], b[N];
FenwickTree fen, fenc;

int main() {
#ifdef local
	freopen("a.in", "r", stdin);
#endif
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", a + i);
		b[a[i]] += 1;
		_a[i] = pii(a[i], i);
	}
	for (int i = n - 1; i; i--) {
		b[i] += b[i + 1];
	}
	F = 1;
	for (int i = 1; i <= n; i++) {
		b[i] = b[i] - (n - i);
		if (b[i] <= 0) {
			puts("0");
			return 0;
		}
		F = F * b[i];
	}
	for (int i = 1; i <= n; i++) {
		if (b[i] == 1) {
			continue;
		}
		if (P[i - 1].v) {
			P[i] = P[i - 1] * Zi(b[i] - 1) * ~Zi(b[i]);
		} else {
			P[i] = Zi(b[i] - 1) * ~Zi(b[i]);
		}
	}
	
	sort(_a + 1, _a + n + 1);
	fen = FenwickTree(n);
	fenc = FenwickTree(n);
	Zi ans = 0;
	int len = 0, lst = 1;
	for (int i = 1; i <= n; i++) {
		int x = _a[i].first, idx = _a[i].second;
		if (i == 1 || x == _a[i - 1].first) {
			len++;
		} else {
			len = 1;
		}
		if (b[x] == 1) {
			lst = x;
			ans += len - 1;
			fen.clear();
			continue;
		}
		while (lst < x) {
			if (b[++lst] == 1) {
				fen.clear();
			}
		}
		ans += fen.query(idx) * P[x];
		fen.add(idx, ~P[x]);
//		cerr << i << " " << (ans * F * inv2).v << '\n';
	}
//	cerr << (ans * F * inv2).v << '\n';
	fen.clear();
	lst = n + 1;
	for (int i = n; i; i--) {
		int x = _a[i].first, idx = _a[i].second;
		if (b[x] == 1) {
			lst = x;
			fen.clear();
			ans += fenc.query(idx) * 2;
		} else {
			while (lst > x) {
				if (b[--lst] == 1) {
					fen.clear();
				}
			}
			ans += fenc.query(idx) * 2 - fen.query(idx) * ~P[x];
			fen.add(idx, P[x]);
		}
		fenc.add(idx, 1);
	}
	ans *= F * inv2;
	printf("%d\n", ans.v);
	return 0;
}
