#include<bits/stdc++.h>
#define title "title"
#define ll long long
#define ull unsigned ll
#define fix(x) fixed<<setprecision(x)
#define pii pair<ll,ll>
#define vint vector<ll>
#define pb push_back
using namespace std;
void Freopen(){
	freopen(title".in","r",stdin);
	freopen(title".out","w",stdout);
}
ll read(){
	ll g=0,f=1;
	char ch=getchar();
	while(ch<'0'||'9'<ch){if(ch=='-')f=-1;ch=getchar();}
	while('0'<=ch&&ch<='9'){g=g*10+ch-'0';ch=getchar();}
	return g*f;
}
const ll N=2e5+5;
const ll mod=1e9+7;
ll n,a[N],cnt[N],inv[N],d[N],zero[N],ztot,pos[N],res=1,ans,inv2;
struct Bit{
	ll su[N],size[N];
	ll lowbit(ll x){return x&(-x);}
	void insert(ll x,ll y){for(ll i=x;i<=n;i+=lowbit(i))su[i]=(su[i]+y)%mod,size[i]++;}
	ll query_su(ll x){ll re=0;for(ll i=x;i;i-=lowbit(i))re=(re+su[i])%mod;return re;}
	ll query_size(ll x){ll re=0;for(ll i=x;i;i-=lowbit(i))re+=size[i];return re;}
	void clear(){memset(su,0,sizeof(su)),memset(size,0,sizeof(size));}
}bit;
ll ksm(ll x,ll y){
	ll re=1;
	while(y){
		if(y&1)re=re*x%mod;
		x=x*x%mod;y>>=1;
	}return re;
}
signed main(){
	n=read();
	for(ll i=1;i<=n;i++)a[i]=read();
	for(ll i=1;i<=n;i++)cnt[a[i]]++;
	for(ll i=n;i>=1;i--)cnt[i-1]+=cnt[i];
	for(ll i=1;i<=n;i++)cnt[i]-=n-i;
	for(ll i=1;i<=n;i++)res=res*cnt[i]%mod;
	d[0]=1;inv2=ksm(2,mod-2);
	for(ll i=1;i<=n;i++){
		ll x=(cnt[i]-1)*ksm(cnt[i],mod-2)%mod;
		if(!x)++ztot,zero[i]=ztot,pos[ztot]=i,d[i]=d[i-1];
		else zero[i]=ztot,d[i]=d[i-1]*x%mod;
		inv[i]=ksm(d[i],mod-2);
	}
	for(ll i=1;i<=n;i++){
		ans=(ans+(bit.query_su(a[i])-bit.query_su(pos[zero[a[i]]])+mod)*d[a[i]]%mod*res%mod*inv2%mod)%mod;
		bit.insert(a[i],inv[a[i]]);
	}bit.clear();
	for(ll i=n;i>=1;i--){
		ans=(ans-(bit.query_su(a[i]-1)-bit.query_su(pos[zero[a[i]]])+mod)*d[a[i]]%mod*res%mod*inv2%mod+mod)%mod;
		ans=(ans+bit.query_size(a[i]-1)*res%mod)%mod;
		bit.insert(a[i],inv[a[i]]);
	}return cout<<ans,signed();
}