#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
#define pb push_back
#define pii pair < int , int >
#define F first
#define S second
#define endl '\n'
#define int long long
#define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#define kill(x) return cout<<x<<'\n', 0;
using namespace std;
const int N=2e5+100,mod=1e9+7;
ll power(ll n,ll k){
    if (k==0) return 1;
    if (k%2==1){
        ll x=power(n,k/2);
        return x*x%mod*n%mod;
    }
    ll x=power(n,k/2);
    return x*x%mod;
}
ll fen[N],fen1[N];
void add1(ll idx,ll val){
    for (;idx<N;idx+= idx & (-idx)){
        fen1[idx]+=val;
        fen1[idx]%=mod;
    }
}
ll get1(ll idx){
    ll s=0;
    for (;idx;idx-= idx & (-idx)) s+=fen1[idx],s%=mod;
    return s;
}
void add(ll idx,ll val){
    for (;idx<N;idx+= idx & (-idx)){
        fen[idx]+=val;
        fen[idx]%=mod;
    }
}
ll get(ll idx){
    ll s=0;
    for (;idx;idx-= idx & (-idx)) s+=fen[idx],s%=mod;
    return s;
}
ll a[N],t[N],b[N];
ll last[N],nxt[N];
int32_t main(){
    ll n;
    cin >> n;
    ll s=1;
    vector <int> ad;
    for (int i=1;i<=n;i++){
        cin >> a[i];
        ad.pb(a[i]);
    }
    sort(ad.begin(),ad.end());
    ll cnt=0;
    for (int i=n;i>0;i--){
        while(ad.size() && ad.back()==i){
            ad.pop_back();
            cnt++;
        }
        s*=cnt-(n-i);
        t[i]=cnt-(n-i);
        s%=mod;
    }
    b[0]=0;
    ll l=1;
    for (int i=1;i<=n;i++){
        b[i]=max((ll)1,b[i-1])*(t[i]-1)%mod*power(t[i],mod-2)%mod;
        if (b[i]==0){
            l=i;
        }
        last[i]=l;
    }
    l=n+1;
    for (int i=n;i;i--){
        if (b[i]==0) l=i;
        nxt[i]=l;
    }
    ll ans=0;
    ll ans1=0;
    for (int i=n;i;i--){
        ll z=get(nxt[a[i]]-1)-get(a[i]-1)+mod;
        z%=mod;
        ll y=power(b[a[i]],mod-2);
        ans+=z*y%mod*power(2,mod-2)%mod*s%mod;
        ans1+=s*(power(2,mod-2)*b[a[i]]%mod*(get1(a[i]-1)-get1(last[a[i]]-1)+mod)%mod)%mod;
        ans%=mod;
        add(a[i],b[a[i]]);
        add1(a[i],power(b[a[i]],mod-2));
        ans%=mod;
        ans1%=mod;
    }
    memset(fen,0,sizeof fen);
    ll ts=0;
    memset(fen1,0,sizeof fen1);
    for (int i=n;i;i--){
        ts+=get1(a[i]-1);
        ts%=mod;
        add1(a[i],1);
    }
    ans+=ts*s%mod-ans1;
    ans+=mod;
    ans%=mod;
    cout << ans%mod << endl;
}
