#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=200005;
const int mod=1000000007;
const ll inv2=(mod+1)>>1;
inline int read()
{
	int x=0,f=1; char ch=getchar();
	for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-1;
	for(;isdigit(ch);ch=getchar()) x=(x<<3)+(x<<1)+ch-'0';
	return (f==1)?x:-x;
}
inline ll fpow(ll a,ll b)
{
	ll ret=1;
	for(;b;b>>=1,a=a*a%mod)
		if(b&1) ret=ret*a%mod;
	return ret;
}
int n,a[maxn],cnt[maxn];
ll p[maxn],S[maxn],invS[maxn],c[maxn][2];
#define lowbit(x) ((x)&(-(x)))
inline void add(int x,ll y) { for(;x<=n;x+=lowbit(x)) (c[x][0]+=y)%=mod,c[x][1]++; }
inline ll sum(int x,int y) { ll ans=0; for(;x;x-=lowbit(x)) (ans+=c[x][y])%=mod; return ans; }
int main()
{
	n=read();
	for(int i=1;i<=n;i++) a[i]=read(),cnt[a[i]]++;
	for(int i=n;i;i--) cnt[i]+=cnt[i+1];
	ll val=1,ans=0;
	for(int i=1;i<=n;i++)
		if(cnt[i]-(n-i)<=0) { puts("0"); return 0; }
		else (val*=cnt[i]-(n-i))%=mod;
	S[0]=1;
	for(int i=1;i<=n;i++)
	{
		ll x=(cnt[i]-(n-i)-1)*fpow(cnt[i]-(n-i),mod-2)%mod;
		if(x==0) S[i]=S[i-1],p[i]=i;
		else S[i]=S[i-1]*x%mod,p[i]=p[i-1];
		invS[i]=fpow(S[i],mod-2);
	}
	for(int i=1;i<=n;i++)
	{	
		(ans+=(sum(a[i],0)-sum(p[a[i]]-1,0))%mod*S[a[i]]%mod*inv2)%=mod;
		add(a[i],invS[a[i]]);
	}
	for(int i=1;i<=n;i++) c[i][0]=c[i][1]=0;
	for(int i=n;i;i--)
	{
		(ans-=(sum(a[i]-1,0)-sum(p[a[i]]-1,0))%mod*S[a[i]]%mod*inv2)%=mod;
		(ans+=sum(a[i]-1,1))%=mod;
		add(a[i],invS[a[i]]);
	}
	printf("%lld\n",(ans*val%mod+mod)%mod);
	return 0;
}