#include<algorithm>
#include<cstdio>
#define N 200002
#define p 1000000007
#define ll long long
using namespace std;
inline int mod(int x){return x>=p?x-p:x;}
inline int calc(int x,int y)
{
	int z=1;
	for(;y;x=(ll)x*x%p,y>>=1)if(y&1)z=(ll)x*z%p;
	return z;
}
int a[N],b[N],c[N],d[N],e[N],f[N],g[N],i,j,n,o[N],s,x;
inline bool cmp(int u,int v){return a[u]==a[v]?u<v:a[u]<a[v];}
inline void add(int u,int v){while(u<=n)o[u]=mod(o[u]+v),u+=u&-u;}
inline int sum(int u)
{
	int v=0;
	while(u)v=mod(o[u]+v),u-=u&-u;
	return v;
}
int main()
{
	scanf("%d",&n),c[n+1]=*d=d[1]=*e=*f=s=1;
	for(i=2;i<=n;i++)d[i]=(ll)(p-p/i)*d[p%i]%p;
	for(i=1;i<=n;i++)scanf("%d",a+i),c[a[b[i]=i]]++;
	for(i=n;i;i--)c[i]=c[i+1]+c[i]-1,s=(ll)c[i]*s%p;
	if(sort(b+1,b+n+1,cmp),!s)return 0*puts("0");
	for(i=1;i<=n;i++)e[i]=(ll)e[i-1]*d[c[i]]%p*(c[i]==1?g[i]=1:c[i]-1)%p,f[i]=(ll)f[i-1]*c[i]%p*d[c[i]-1]%p,g[i]+=g[i-1];
	for(i=j=1;i<=n;x=(x+(ll)sum(b[i])*e[a[b[i]]])%p,add(b[i],f[a[b[i]]]),i++)if(g[a[b[i]]]!=g[a[b[j]]])while(j<i)add(b[j],mod(p-f[a[b[j]]])),j++;
	for(i=1;i<=n;i++)o[i]=0;
	for(i=j=n;i;x=(x+(ll)sum(b[i])*f[a[b[i]]])%p,add(b[i],mod(p-e[a[b[i]]])),i--)if(g[a[b[i]]]!=g[a[b[j]]])while(i<j)add(b[j],e[a[b[j]]]),j--;
	for(i=1;i<=n;i++)o[i]=0;
	for(i=j=n;i;x=mod(x+sum(b[i])),add(b[i],2),i--);
	return 0*printf("%lld\n",500000004ll*x%p*s%p);
}