#include<bits/stdc++.h>
using namespace std;

#define int long long

#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;

template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

const int mod=1000000007;
inline void add(int &a,int b){
    a+=b;
    if(a>=mod)a-=mod;
}
int mpow(int n,int m){
    int ret=1;
    while(m){
        if(m&1)ret=ret*n%mod;
        n=n*n%mod;
        m>>=1;
    }
    return ret;
}
const int FACT_SIZE=1111111;
int fact[FACT_SIZE];
int inv[FACT_SIZE];
struct fact_exec{
    fact_exec(){
        fact[0]=1;
        for(int i=1;i<FACT_SIZE;i++)fact[i]=fact[i-1]*i%mod;
        inv[FACT_SIZE-1]=mpow(fact[FACT_SIZE-1],mod-2);
        for(int i=FACT_SIZE-2;i>=0;i--)inv[i]=inv[i+1]*(i+1)%mod;
    }
}factexec;
int nCk(int n,int k){
    if(n<0|k<0||k>n)return 0;
    return fact[n]*inv[k]%mod*inv[n-k]%mod;
}
int nPk(int n,int k){
    if(n<0||k<0||k>n)return 0;
    return fact[n]*inv[n-k]%mod;
}


struct BinaryIndexedTree{
    int n;
    vector<int>dat;
    BinaryIndexedTree(int n=0):n(n){
        dat.resize(n+1);
    }
    void add(int k,int x){
        for(k++;k<=n;k+=k&-k)dat[k]=(dat[k]+x)%mod;
    }
    int sum(int k){
        int ret=0;
        for(k++;k;k-=k&-k)ret=(ret+dat[k])%mod;
        return ret;
    }
};

int al;


int solve(vint A){
    int N=A.size();

    vint S(N);
    rep(i,N)S[A[i]]++;
    for(int i=N-1;i>0;i--)S[i-1]+=S[i];
    rep(i,N)S[i]-=N-1-i;

    vint MS=S,MS2=S;
    for(int i=1;i<N;i++)MS2[i]--;
    for(int i=1;i<N;i++){
        MS[i]=MS[i]*MS[i-1]%mod;
        MS2[i]=MS2[i]*MS2[i-1]%mod;
    }

    BinaryIndexedTree bit(N);
    BinaryIndexedTree bit2(N);
    BinaryIndexedTree bit3(N);
    int ret=0;
    for(int i=0;i<N;i++){
        int tmp=bit.sum(A[i]);
        tmp=tmp*MS2[A[i]]%mod*mpow(MS[A[i]],mod-2)%mod;
        tmp=tmp*inv[2]%mod;
        add(ret,tmp*al%mod);

        tmp=bit2.sum(N-1)-bit2.sum(A[i])+mod;
        tmp%=mod;
        tmp=tmp*MS[A[i]]%mod*mpow(MS2[A[i]],mod-2)%mod;
        tmp=tmp*al%mod;

        add(ret,tmp*inv[2]%mod);
        add(ret,(al*(i-bit3.sum(A[i]))%mod-tmp+mod)%mod);

        bit.add(A[i],MS[A[i]]*mpow(MS2[A[i]],mod-2)%mod);
        bit2.add(A[i],MS2[A[i]]*mpow(MS[A[i]],mod-2)%mod);
        bit3.add(A[i],1);
    }
    return ret;
}

signed main(){
    int N;cin>>N;
    vint A(N);rep(i,N)cin>>A[i],A[i]--;
    vint S(N);
    rep(i,N)S[A[i]]++;
    for(int i=N-1;i>0;i--)S[i-1]+=S[i];
    rep(i,N)S[i]-=N-1-i;

    al=1;
    rep(i,N)al=al*S[i]%mod;

    if(*min_element(all(S))<=0){
        cout<<0<<endl;
        return 0;
    }


    int ans=0;
    vector<vint>lis(N);
    rep(i,N)lis[A[i]].pb(i);
    BinaryIndexedTree bit(N);
    int pre=N;
    for(int i=N-1;i>=0;i--){
        if(S[i]!=1)continue;
        vint idx;
        for(int j=i;j<pre;j++)for(auto k:lis[j])idx.pb(k);
        sort(all(idx));
        for(auto k:idx)add(ans,bit.sum(k)*al%mod);
        for(auto k:idx)bit.add(k,1);

        vint a(idx.size());
        rep(j,idx.size())a[j]=A[idx[j]]-i;
        add(ans,solve(a));
        pre=i;
    }

    cout<<ans<<endl;
    return 0;
}
