#include<bits/stdc++.h>
using namespace std;
#define cs const
#define pb push_back
#define ll long long 
#define pii pair<int,int>
#define fi first
#define se second
#define bg begin
cs int RLEN=(1<<20)|3;
inline char gc(){
	static char ibuf[RLEN],*ib,*ob;
	(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
	return (ib==ob)?EOF:*ib++;
}
inline int read(){
	char ch=gc();
	int res=0;bool f=1;
	while(!isdigit(ch))f^=ch=='-',ch=gc();
	while(isdigit(ch))res=(res*10)+(ch^48),ch=gc();
	return f?res:-res;
}
inline ll readll(){
	char ch=gc();
	ll res=0;bool f=1;
	while(!isdigit(ch))f^=ch=='-',ch=gc();
	while(isdigit(ch))res=(res*10)+(ch^48),ch=gc();
	return f?res:-res;	
}
inline int readstring(char *s){
	char ch=gc();int l=0;
	while(isspace(ch))ch=gc();
	while(!isspace(ch)&&ch!=EOF)s[++l]=ch,ch=gc();
	return l;
}
inline char readchar(){
	char ch=gc();
	while(isspace(ch))ch=gc();return ch;
}
template<typename tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<typename tp>inline void chemn(tp &a,tp b){a<b?a=b:0;}
cs int mod=1e9+7;
inline int add(int a,int b){return (a+b>=mod)?(a+b-mod):(a+b);}
inline int dec(int a,int b){return (a<b)?(a-b+mod):(a-b);}
inline int mul(int a,int b){static ll r;r=(ll)a*b;return (r>=mod)?(r%mod):r;}
inline void Add(int &a,int b){a=(a+b>=mod)?(a+b-mod):(a+b);}
inline void Dec(int &a,int b){a=(a<b)?(a-b+mod):(a-b);}
inline void Mul(int &a,int b){static ll r;r=(ll)a*b;a=(r>=mod)?(r%mod):r;}
inline int ksm(int a,int b,int res=1){for(;b;b>>=1,Mul(a,a))(b&1)&&(Mul(res,a),1);return res;}
inline int Inv(int x){return ksm(x,mod-2);} 
cs int N=200005,iv2=Inv(2);
int n,a[N],cnt[N],pre[N],sm[N],is[N];
namespace bit{
	int tr1[N],tr2[N];
	#define lb(x) (x&(-x))
	void update(int p,int k){for(;p<=n;p+=lb(p))Add(tr1[p],k),tr2[p]++;}
	int query(int p,int res=0){for(;p>0;p-=lb(p))Add(res,tr1[p]);return res;}
	int query2(int p,int res=0){for(;p>0;p-=lb(p))Add(res,tr2[p]);return res;}
	void clear(){memset(tr1,0,sizeof(tr1)),memset(tr2,0,sizeof(tr2));}
	#undef lb
}
int main(){
	#ifdef Stargazer
	freopen("lx.in","r",stdin);
	#endif
	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];int all=1;
	for(int i=1;i<=n;i++)cnt[i]-=n-i,Mul(all,cnt[i]);
//	cout<<all<<'\n';
	sm[0]=1,pre[0]=1;
	for(int i=1;i<=n;i++){
		int vl=mul(cnt[i]-1,Inv(cnt[i]));
		if(!vl)pre[i]=i,sm[i]=sm[i-1];
		else pre[i]=pre[i-1],sm[i]=mul(sm[i-1],vl);
		is[i]=Inv(sm[i]);
	}int res=0;
	for(int i=1;i<=n;i++){
		Add(res,mul(sm[a[i]],mul(all,mul(iv2,dec(bit::query(a[i]),bit::query(pre[a[i]]-1))))));
		bit::update(a[i],is[a[i]]);
	}
	bit::clear();
	for(int i=n;i;i--){
		Dec(res,mul(sm[a[i]],mul(all,mul(iv2,dec(bit::query(a[i]-1),bit::query(pre[a[i]]-1))))));
		Add(res,mul(all,bit::query2(a[i]-1)));
		bit::update(a[i],is[a[i]]);
	}cout<<res<<'\n';return 0;
}