#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define mp make_pair
#define PI pair<int,int>
#define poly vector<ll>
#define mem(a) memset((a),0,sizeof(a))
#define For(i,l,r) for(int i=(int)(l);i<=(int)(r);i++)
#define Rep(i,r,l) for(int i=(int)(r);i>=(int)(l);i--)
#define pb push_back
#define fi first
#define se second
inline char gc(){
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
#define gc getchar
inline ll read(){
	ll x = 0; char ch = gc(); bool positive = 1;
	for (; !isdigit(ch); ch = gc())	if (ch == '-')	positive = 0;
	for (; isdigit(ch); ch = gc())	x = x * 10 + ch - '0';
	return positive ? x : -x;
}
inline void write(ll a){
    if(a<0){
    	a=-a; putchar('-');
	}
    if(a>=10)write(a/10);
    putchar('0'+a%10);
}
inline void writeln(ll a){write(a); puts("");}
inline void wri(ll a){write(a); putchar(' ');}
inline ull rnd(){
	return ((ull)rand()<<30^rand())<<4|rand()%4;
}
const int N=200005,mod=1e9+7;
int to[N],s[N],a[N],jb[N];
ll g[N],f[N],c[N],myh,sum=1;
ll ksm(ll a,int b){
	int ans=1;
	for(;b;b>>=1){
		if(b&1)ans=ans*a%mod;
		a=a*a%mod;
	}
	return ans;
}
#define lowbit(i) i&-i
ll ask(int pos){
	ll ans=0;
	for(int i=pos;i;i-=lowbit(i))ans+=c[i];
	return ans;
}
void change(int pos,int de){
	for(int i=pos;i<N;i+=lowbit(i))c[i]+=de;
}
int main(){
	int n=read();
	For(i,1,n)to[a[i]=read()]++;
	Rep(i,n,1){
		to[i]+=to[i+1];
		sum=sum*(s[i]=to[i]-(n-i))%mod;
	}
	For(i,f[0]=g[0]=1,n){
		f[i]=f[i-1]; jb[i]=jb[i-1];
		if(s[i]>1)f[i]=f[i]*(s[i]-1)%mod*ksm(s[i],mod-2)%mod;
		 else jb[i]=i-1;
		g[i]=ksm(f[i],mod-2);
	}
	For(i,1,n){
		change(a[i],1);
		myh+=i-ask(a[i]);
	}
	mem(c);
	ll ans=myh*2%mod;
	For(i,1,n){
		ans=(ans+(ask(a[i])-ask(jb[a[i]]))%mod*f[a[i]])%mod;
		change(a[i],g[a[i]]);
	}
	mem(c);
	Rep(i,n,1){
		ans=(ans-(ask(a[i]-1)-ask(jb[a[i]]))%mod*f[a[i]])%mod;
		change(a[i],g[a[i]]);
	}
	//cout<<ans-2*myh<<" "<<myh<<" "<<ans<<endl;
	ans=(ans*(mod+1)/2%mod+mod)%mod;
	cout<<ans*sum%mod<<endl;
}

/*
Taj/Tai 
*/