#include <cstdio>
#include <cassert>
#include <algorithm>

using namespace std;

typedef pair<int, int> PII;

#define fi first
#define se second

#define lf else if

namespace io
{
	const int maxb = 1 << 15;
	char b[maxb], *s = b, *t = b;
	bool Getchar(char &ch)
	{
		return ch = s == t && (t = (s = b) + fread(b, 1, maxb, stdin)) == b ? 0 : *s ++;
	}
	int Getint()
	{
		char ch;
		while (Getchar(ch) && (ch < '0' || ch > '9'));
		int s = ch - '0';
		while (Getchar(ch) && ch >= '0' && ch <= '9')
			s = s * 10 + ch - '0';
		return s;
	}
}

using io::Getint;

const int maxn = 2e5 + 10;
const int Mod = 1e9 + 7;
const int inv2 = (Mod + 1) / 2;

void Chkadd(int &x, const int &y) { if ((x += y) >= Mod) x -= Mod; }
void Chksub(int &x, const int &y) { if ((x -= y) < 0) x += Mod; }

int Power(int a, int k)
{
	int x = 1;
	for (; k; k >>= 1)
	{
		if (k & 1) x = 1ll * x * a % Mod;
		a = 1ll * a * a % Mod;
	}
	return x;
}

int n;
int A[maxn], cnt[maxn], S;
PII D[maxn], tree[maxn], iD[maxn];

PII operator * (const PII &a, const int &b)
{
	return b ? PII(1ll * a.fi * b % Mod, a.se) : PII(a.fi, a.se + 1);
}

void operator += (PII &a, const PII &b)
{
	if (a.se < b.se) a = b;
	lf (a.se == b.se) Chkadd(a.fi, b.fi);
}

void BIT_init()
{
	for (int i = 1; i <= n; ++ i) tree[i] = PII(0, 0);
}

void Update(int x, PII del)
{
	for (; x <= n; x += x & -x) tree[x] += del;
}

PII Query(int x)
{
	PII res(0, 0);
	if (!x) return res;
	for (; x ; x -= x & -x) res += tree[x];
	return res;
}

int tr[maxn];

void Insert(int x)
{
	for (; x <= n; x += x & -x) ++ tr[x];
}

int Getsum(int x)
{
	if (!x) return 0;
	int res = 0;
	for (; x; x -= x & (-x)) res += tr[x];
	return res;
}

int main()
{
	n = Getint();
	for (int i = 1; i <= n; ++ i)
	{
		A[i] = Getint();
		++ cnt[A[i]];
	}
	for (int i = n; i; -- i) cnt[i] += cnt[i + 1];
	S = 1;
	for (int i = n; i; -- i)
	{
		cnt[i] -= n - i;
		if (cnt[i] <= 0) return puts("0"), 0;
		S = 1ll * S * cnt[i] % Mod;
	}
	D[0] = PII(1, 0);
	for (int i = 1; i <= n; ++ i)
	{
		D[i] = D[i - 1] * (1ll * (cnt[i] - 1) * Power(cnt[i], Mod - 2) % Mod);
		iD[i] = PII(Power(D[i].fi, Mod - 2), D[i].se);
	}
	
	int ans = 0;
	
	for (int i = 1; i <= n; ++ i)
	{
		int cur = A[i];
		PII res = Query(cur);
		assert(res.se <= D[cur].se);
		if (res.se == D[cur].se)
			Chkadd(ans, 1ll * D[cur].fi * res.fi % Mod);
		Update(cur, iD[cur]);
	}
	
	BIT_init();
	
	for (int i = n; i; -- i)
	{
		int cur = A[i];
		PII res = Query(cur - 1);
		assert(res.se <= D[cur].se);
		if (res.se == D[cur].se)
			Chksub(ans, 1ll * D[cur].fi * res.fi % Mod);
		Update(cur, iD[cur]);
		
		Chkadd(ans, 2ll * Getsum(cur - 1) % Mod);
		Insert(cur);
	}
	
	ans = 1ll * ans * S % Mod * inv2 % Mod;
	
	printf("%lld\n", ans);
	
	return 0;
}