#include<cstdio>
#include<algorithm>
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int,int> PA;
const int N=200005,p=1000000007;
PA T[N],Z[N],FZ[N];
int ni[N],C[N],t[N],A[N],H[N],i,j,k,n,m,an,R;
char c;

inline char getc()
{
	#define VV 10000000
	static char s[VV],*l=s,*r=s;
	if (l==r)
		l=s,r=s+fread(s,1,VV,stdin);
	return l==r?0:*l++;
}

int read(){ int z=0; do c=getc(); while (c<'0'||c>'9'); while (c>='0'&&c<='9') z*=10,z+=c-'0',c=getc(); return z; }

void Plus(int &a,int b){ a+=b,a-=a<p?0:p; }
void Minus(int &a,int b){ a-=b,a+=a<0?p:0; }

PA operator *(const PA &A,const int &b)
{
	return b?PA((LL)A.fi*b%p,A.se):PA(A.fi,A.se+1);
}

void operator +=(PA &A,const PA &B)
{
	if (A.se<B.se)
		A=B;
	else
		if (A.se==B.se)
			Plus(A.fi,B.fi);
}

int power(int z,int k)
{
	int d=1;
	for (;k;z=(LL)z*z%p,k>>=1) if (k&1) d=(LL)d*z%p;
}

PA mt(int w,PA z)
{
	for (;w<=n;w+=w&-w) T[w]+=z;
}

PA qt(int w)
{
	PA z=PA(0,0);
	for (;w;w-=w&-w) z+=T[w];
	return z;
}

void add(int w){ for (;w<=n;w+=w&-w) ++t[w]; }
int getsum(int w){ int z=0; for (;w;w-=w&-w) z+=t[w]; return z; }

int solve()
{
	ni[2]=p+1>>1;
	n=read(),R=1;
	for (i=1;i<=n;++i) ++C[A[i]=read()];
	for (i=n;i;--i)
	{
		C[i]+=C[i+1],H[i]=C[i]-(n-i);
		if (H[i]<=0) return 0;
		R=(LL)R*H[i]%p;
	}
	for (ni[1]=1,i=2;i<=n;++i) ni[i]=p-(LL)(p/i)*ni[p%i]%p;
	ni[0]=0;
	Z[0]=FZ[0]=PA(1,0);
	for (i=1;i<=n;++i)
		Z[i]=Z[i-1]*((LL)(H[i]-1)*ni[H[i]]%p),
		FZ[i]=FZ[i-1]*((LL)H[i]*ni[H[i]-1]%p);
	
	for (i=1;i<=n;++i)
	{
		int d=A[i];
		PA D=qt(d);
		if (D.se==Z[d].se)
			Plus(an,(LL)D.fi*Z[d].fi%p);
		mt(d,FZ[d]);
	}
	for (i=1;i<=n;++i) T[i]=PA(0,0);
	for (i=n;i;--i)
	{
		int d=A[i];
		PA D=qt(d-1);
		if (D.se==Z[d].se)
			Minus(an,(LL)D.fi*Z[d].fi%p);
		mt(d,FZ[d]);
		Plus(an,(getsum(d-1)<<1)%p),
		add(d);
	}
	
	return (LL)an*R%p*ni[2]%p;
}

int main()
{
	printf("%d\n",solve());
	return 0;
}