
#include <cstdio>
#include <cstring>

using namespace std;
const int Max_N(200050);
const int MOD(1000000000 + 7);

namespace io
{
	const int SIZE = (1 << 21) + 1;
	char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr;
	#define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
	inline void flush (){fwrite (obuf, 1, oS - obuf, stdout);oS = obuf;}
	inline void putc (char x){*oS ++ = x;if (oS == oT) flush ();}
	template <class I>
	inline void gi (I &x) {for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;
		for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;}
	template <class I>
	inline void print (I x){
		if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;while(x) qu[++ qr] = x % 10 + '0',  x /= 10;while (qr) putc (qu[qr--]);}
	struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
}
using io::gi;
using io::putc;
using io::print;

constexpr int Mult(int a, int b)
{
	return a * 1LL * b % MOD;
}

constexpr int Add(int a, int b)
{
	return a + b >= MOD ? a + b - MOD : a + b;
}

constexpr int Sub(int a, int b)
{
	return a - b < 0 ? a - b + MOD : a - b;
}

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

inline int inverse(int a)
{
	int invx, invy;
	exgcd(a, MOD, invx, invy);
	return (invx % MOD + MOD) % MOD;
}

int N, A[Max_N], C[Max_N], Fac[Max_N], Inv[Max_N], Ans, Cnt, inv2, Old[Max_N], Newa[Max_N], Newb[Max_N], Newm[Max_N], Pre[Max_N], Lst0[Max_N];

inline int work(int l, int r)
{
	return l > r ? 1 : (1 <= l && r <= N ? Mult(Fac[r], Inv[l - 1]) : 0);
}

int BIT[Max_N];

constexpr int lowbit(int x)
{
	return x & -x;
}

void insert(int i, int v)
{
	while (i)
		BIT[i] = Add(BIT[i], v), i -= lowbit(i);
}

int query(int i)
{
	int Ret(0);
	while (i <= N)
		Ret = Add(Ret, BIT[i]), i += lowbit(i);
	return Ret;
}

int main()
{
	inv2 = inverse(2);
	gi(N);
	for (int i = 1;i <= N;++i)
		gi(A[i]), ++C[A[i]];
	for (int x = 1;x <= N;++x)
		C[x] += C[x - 1];
	Fac[0] = 1;
	for (int i = 1;i <= N;++i)
		Fac[i] = Mult(Fac[i - 1], i);
	Inv[N] = inverse(Fac[N]);
	for (int i = N - 1;i >= 0;--i)
		Inv[i] = Mult(Inv[i + 1], i + 1);
	Cnt = 1;
	Pre[0] = 1;
	for (int x = 1;x <= N;++x)
	{
		Old[x] = work(x - C[x] + 1, x - C[x - 1]);
		if (Old[x] == 0)
		{
			printf("0");
			return 0;
		}
		Cnt = Mult(Cnt, Old[x]);
		Newa[x] = Mult(inverse(Old[x]), work(x - C[x], x - C[x - 1]));
		Newb[x] = Mult(inverse(Old[x]), work(x - C[x] + 1, x - C[x - 1] - 1));
		Newm[x] = Mult(inverse(Old[x]), work(x - C[x], x - C[x - 1] - 1));
		Lst0[x] = (Newm[x] == 0 ? x : Lst0[x - 1]);
		Pre[x] = Mult(Pre[x - 1], Newm[x] == 0 ? 1 : Newm[x]);
	}
	for (int x = 1;x <= N;++x)
		Ans = Add(Ans, Mult(Mult(Mult((C[x] - C[x - 1]), (C[x] - C[x - 1]) - 1), inv2), Mult(Cnt, inv2)));
	for (int i = 1, j;i <= N;++i)
	{
		j = Lst0[A[i] - 1];
		if (j == 0)
			j = 1;
		Ans = Add(Ans, Mult(inv2, Mult(Cnt, Mult(Sub(query(j), query(A[i])), Mult(Newb[A[i]], Pre[A[i] - 1])))));
		insert(A[i], Mult(Newa[A[i]], inverse(Pre[A[i]])));
	}
	memset(BIT, 0, sizeof(BIT));
	for (int i = N, j;i >= 1;--i)
	{
		j = Lst0[A[i] - 1];
		if (j == 0)
			j = 1;
		Ans = Sub(Ans, Mult(inv2, Mult(Cnt, Mult(Sub(query(j), query(A[i])), Mult(Newb[A[i]], Pre[A[i] - 1])))));
		insert(A[i], Mult(Newa[A[i]], inverse(Pre[A[i]])));
	}
	memset(BIT, 0, sizeof(BIT));
	for (int i = N, j;i >= 1;--i)
	{
		Ans = Add(Ans, Mult(Cnt, Sub(query(1), query(A[i]))));
		insert(A[i], 1);
	}
	print(Ans);
	return 0;
}