#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define ll long long
#define MAX 200200
#define MOD 1000000007
#define inv2 500000004
inline int read()
{
	int x=0;bool t=false;char ch=getchar();
	while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
	if(ch=='-')t=true,ch=getchar();
	while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
	return t?-x:x;
}
int fpow(int a,int b)
{
	int s=1;
	while(b){if(b&1)s=1ll*s*a%MOD;a=1ll*a*a%MOD;b>>=1;}
	return s;
}
int n,a[MAX],tot,ans;
int cnt[MAX];
int c1[MAX],c2[MAX];
int lb(int x){return x&(-x);}
void add(int x,int w){while(x<=n)c1[x]=(c1[x]+w)%MOD,++c2[x],x+=lb(x);}
int getsum1(int x){int ret=0;while(x)ret=(ret+c1[x])%MOD,x-=lb(x);return ret;}
int getsum2(int x){int ret=0;while(x)ret+=c2[x],x-=lb(x);return ret;}
int D[MAX],invD[MAX],zero[MAX],S[MAX];
int main()
{
	n=read();tot=1;
	for(int i=1;i<=n;++i)++cnt[a[i]=read()];
	for(int i=n-1;i;--i)cnt[i]+=cnt[i+1];
	for(int i=1;i<=n;++i)tot=1ll*tot*(cnt[i]-=(n-i))%MOD;
	if(!tot){puts("0");return 0;}
	S[0]=D[0]=1;
	for(int i=1;i<=n;++i)
	{
		int x=1ll*(cnt[i]-1)*fpow(cnt[i],MOD-2)%MOD;
		if(!x)S[zero[i]=zero[i-1]+1]=i,D[i]=D[i-1];
		else zero[i]=zero[i-1],D[i]=1ll*D[i-1]*x%MOD;
		invD[i]=fpow(D[i],MOD-2);
	}
	for(int i=1;i<=n;++i)
	{
		ans=(ans+1ll*(getsum1(a[i])-getsum1(S[zero[a[i]]]-1)+MOD)*D[a[i]]%MOD*tot%MOD*inv2%MOD)%MOD;
		add(a[i],invD[a[i]]);
	}
	for(int i=1;i<=n;++i)c1[i]=c2[i]=0;
	for(int i=n;i;--i)
	{
		ans-=1ll*(getsum1(a[i]-1)-getsum1(S[zero[a[i]]]-1)+MOD)*D[a[i]]%MOD*tot%MOD*inv2%MOD;
		ans=(ans+1ll*getsum2(a[i]-1)*tot)%MOD;ans=(ans+MOD)%MOD;
		add(a[i],invD[a[i]]);
	}
	printf("%d\n",ans);
	return 0;
}
