#include<bits/stdc++.h>
#define ll long long
#define rg register
using namespace std;
const int MAXN=2e5+10;
const int Mod=1e9+7;
int n,Ans,A[MAXN];
int L1[MAXN]={1},Il1[MAXN]={1},L2[MAXN]={1},Il2[MAXN]={1};
int Prez[MAXN],Inv2=500000004;
inline int Read()
{   rg int a=0,c=1;   rg char b=getchar();
	while(b!='-'&&(b<'0'||b>'9'))b=getchar();
	if(b=='-')c=-1,b=getchar();
	while(b>='0'&&b<='9')a=a*10+b-48,b=getchar();
	return a*c;
}
inline int Fast(rg int Di,rg int Mi)
{   rg int Back=1;
	while(Mi>=1)
	{   if(Mi&1) Back=1ll*Back*Di%Mod;
		Di=1ll*Di*Di%Mod;   Mi>>=1;
	}
	return Back;
}
inline int Calc(rg int Le,rg int Ri)
{   if(Prez[Ri]>=Le) return 0;
	else return 1ll*L1[n]*Il1[Ri]%Mod*L2[Ri]%Mod*Il2[Le-1]%Mod*L1[Le-1]%Mod*Inv2%Mod;
}
int main()
{   n=Read();
	for(rg int i=1;i<=n;i++) A[i]=Read(),L1[A[i]]++;
	for(rg int i=n;i>=1;i--) L1[i]+=L1[i+1];
	for(rg int i=1;i<=n;i++) L1[i]-=n-i,L2[i]=L1[i]-1;
	for(rg int i=1;i<=n;i++)
	{   Prez[i]=L2[i]?Prez[i-1]:i;
		L1[i]=1ll*L1[i-1]*L1[i]%Mod;
		L2[i]=(L2[i]?1ll*L2[i-1]*L2[i]%Mod:L2[i-1]);
		Il1[i]=Fast(L1[i],Mod-2),Il2[i]=Fast(L2[i],Mod-2);
	}
	for(rg int i=1;i<=n;i++)
		for(rg int j=i+1;j<=n;j++)
		{   if(A[i]==A[j]) Ans=(Ans+1ll*L1[n]*Inv2)%Mod;
			else if(A[i]<A[j]) Ans=(Ans+Calc(A[i]+1,A[j]))%Mod;
			else Ans=(1ll*Ans+L1[n]-Calc(A[j]+1,A[i])+Mod)%Mod;
		}
	printf("%d\n",Ans);
}
