#include <bits/stdc++.h>

typedef long long int int64;
static const int64 mod = 1000000007LL;
using pair = std::pair<int64,int>;
int64 exgcd(int64 a,int64 b,int64 &c,int64 &d)
{
	if(b == 0) {
		c = 1,d = 0;
		return a;
	}
	int64 v = exgcd(b,a%b,d,c);
	d -= c * (a / b);
	return v;
}
int64 finv(int64 x)
{
	int64 a,b;
	exgcd(x,mod,a,b);
	return a < 0 ? a + mod : a;
}
pair operator *(const pair &a,const pair &b) {
	return pair(a.first*b.first%mod,a.second+b.second);
}
pair operator +(const pair &a,const pair &b) {
	if(a.second == b.second) {
		return pair(a.first*b.first%mod,a.second);
	} else if(a.second > b.second) {
		return a;
	} else {
		return b;
	}
}
 arr[200003];
void add(int i,pair p)
{
	while(i <= 200000) {
		arr[i] = arr[i] + p;
		i += i&-i;
	}
}
pair sum(int i)
{
	pair ans(1,0);
	while(i > 0) {
		ans = ans + arr[i];
		i -= i&-i;
	}
	return ans;
}
int n;
int a[200003];
int cnt[200003];
pair d[200003];
int main()
{
	scanf("%d",&n);
	for(int i = 0;i < n;++i) {
		scanf("%d",&a[i]);
		++cnt[a[i]];
	}
	for(int i = n-1;i >= 1;--i) {
		cnt[i] = cnt[i] + cnt[i+1] - 1;
		if(cnt[i] <= 0) {
			printf("0\n");
			return 0;
		}
	}
	for(int i = 0;i < n;++i) {
		pair cur,cur2;
		if(cnt[i] == 1) {
			cur = pair(1,1);
			cur2 = pair(1,-1);
		} else {
			cur = pair((int64)(cnt[i]-1)*finv(cnt[i])%mod,0);
			cur2 = pair((int64)cnt[i]*finv(cnt[i]-1)%mod,0);
		}
		if(i == 0) {
			d[i] = cur;
			d2[i] = cur2;
		} else {
			d[i] = d[i-1] * cur;
			d2[i] = d2[i-1] * cur2;
		}
	}
	int64 ans = 0;
	for(int i = 0,j = 0;i < n;i = j) {
		while(j < n && d[j].second == d[i].second) ++j;
		for(int k = i;k < j;++k) {
		}
		pair s = sum(a[i]);
		add(a[i],-d[i]);
	}
}
