#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
ll mod = 1000000007;
#define SIZE 262144
ll inv[SIZE + 1];
ll kai[SIZE + 1];
ll invkai[SIZE + 1];
void invinit()
{
	inv[1] = 1;
	for (int i = 2; i <= SIZE; i++)
	{
		inv[i] = mod - (mod / i)*inv[mod%i] % mod;
	}
	kai[0] = invkai[0] = 1;
	for (int i = 1; i <= SIZE; i++)
	{
		kai[i] = kai[i - 1] * i%mod;
		invkai[i] = invkai[i - 1] * inv[i] % mod;
	}
}
ll com(ll a, ll b)
{
	if (b < 0 || a < b)return 0;
	return (invkai[b] * invkai[a - b]) % mod*kai[a] % mod;
}
ll po(ll a, ll b)
{
	if (b == 0)return 1;
	ll z = po(a, b / 2);
	z = z*z%mod;
	if (b & 1)z = z*a%mod;
	return z;
}
ll getinv(ll a)
{
	return po(a, mod - 2);
}
class BIT
{
public:
	ll bit[SIZE + 1];
	void add(int a, ll b)
	{
		a++;
		for (;;)
		{
			bit[a] = (bit[a] + b) % mod;
			a += a&-a;
			if (a > SIZE)return;
		}
	}
	ll get(int a)
	{
		a++;
		ll ret = 0;
		for (;;)
		{
			ret += bit[a];
			a -= a&-a;
			if (a == 0)return ret%mod;
		}
	}
};
BIT glo, bi1, bi2;
typedef pair<ll, ll>pii;
int dat[202020];
ll tim[202020], rui[202020], gg[202020];
int main()
{
	int num;
	scanf("%d", &num);
	vector<pii>zv;
	invinit();
	for (int i = 0; i < num; i++)
	{
		scanf("%d", &dat[i]);
		zv.push_back(make_pair(dat[i], i));
	}
	sort(zv.begin(), zv.end());
	int now = 0, st = 0;
	ll ans = 0, cnt = 1;
	for (int i = 0; i < num; i++)
	{
		int k = zv[i].second;
		if (dat[k] <= i)
		{
			printf("0\n");
			return 0;
		}
		cnt = cnt*(dat[k] - i) % mod;
		ll pr;
		if (now - i == 0)
		{
			pr = inv[dat[k] - now];
			tim[k] = 1;
		}
		else
		{
			pr = (rui[now] + mod - rui[st])*inv[now - i] % mod;
			tim[k] = getinv(pr)*inv[dat[k] - i] % mod;
		}
		for (int j = now + 1; j <= dat[k]; j++)rui[j] = (rui[j - 1] + pr) % mod;
		now = dat[k];
		if (dat[k] == i + 1)
		{
			ll sum = 0;
			for (int j = i; j >= st; j--)
			{
				int x = zv[j].second;
				ll kak = (rui[dat[x]] + mod - rui[st]) % mod;
				ll s1 = bi1.get(x), s2 = bi1.get(num) + mod - bi1.get(x);
				ll n1 = bi2.get(x);
				ll r1 = n1 + mod - s1*kak%mod*((mod + 1) / 2) % mod;
				ll r2 = s2*kak%mod*((mod + 1) / 2) % mod;
				sum += r1 + r2;
				bi1.add(x, tim[x]);
				bi2.add(x, 1);
			}
			ans = (ans + sum) % mod;
			for (int j = i; j >= st; j--)
			{
				ll x = zv[j].second;
				bi1.add(x, mod - tim[x]);
				bi2.add(x, mod - 1);
				gg[x] = st;
			}
			st = now;
		}
	}
	ll bas = 0;
	for (int i = 0; i < num; i++)
	{
		bas += glo.get(num) + mod - glo.get(gg[i]);
		glo.add(gg[i], 1);
	}
	printf("%lld\n", (bas + ans) % mod*cnt%mod);
}