#include <iostream>
#include<bits/stdc++.h>

typedef long long ll;
using namespace std;

const ll MOD = 1000000007;
const ll maxn = 200005;
ll t[maxn];
ll t2[maxn];
ll inv[maxn];

void add(ll t[], ll i, ll value) {
  for (; i < maxn; i += (i + 1) & -(i + 1))
    t[i] = (t[i] + value)%MOD;
}

// sum[0..i]
ll sum(ll t[], ll i) {
  ll res = 0;
  for (; i >= 0; i -= (i + 1) & -(i + 1))
    res = (res + t[i])%MOD;
  return res;
}

ll exp(ll a, ll b){
    if(b==0)
        return 1;
    else if (b%2==0){
        ll y = exp(a,b/2);
        return (y*y)%MOD;
    }
    return (a*exp(a,b-1))%MOD;
}

int main()
{
    ll N;
    cin>>N;
    ll A[N];
    for(ll i=0; i<N; i++){
        cin>>A[i];
    }
    ll freq[N+1];
    for(ll i=0; i<N+1; i++){
        freq[i] = 0;
    }
    for(ll i=0; i<N; i++){
        freq[A[i]]++;
    }
    for(ll i=N-1; i>=0; i--){
        freq[i] = freq[i+1]+freq[i]-1;
    }

    for(ll i=1; i<N+1; i++){
        if(freq[i]==0){
            cout<<0<<endl;
            return 0;
        }
    }

    ll partial[N+1];
    ll check[N+1];
    ll pcheck[N+1];
    for(ll i=0; i<N+1; i++)
        check[i] = 0;
    partial[0] = 1;
    for(ll i=1; i<N+1; i++){
        partial[i] = (partial[i-1]*(freq[i]-1))%MOD;
        partial[i] = (partial[i] * exp(freq[i],MOD-2))%MOD;
        if(partial[i]==0){
            partial[i] = 1;
            check[i] = 1;
            // signify 0
        }
    }
    pcheck[0] = 0;
    ll u = 0;
    for(ll i=1; i<N+1; i++){
        if(check[i]==1){
            for(ll j=u; j<i; j++){
                pcheck[j] = i;
            }
            u = i;
        }
    }
    for(ll j=u; j<N+1; j++){
        pcheck[j] = N+1;
    }

    ll ans = 0;
    for(ll i=0; i<N; i++){
        ll carry = sum(t,A[i]);
        ans =  (ans + partial[A[i]]*carry)%MOD;
        add(t,A[i],exp(partial[A[i]],MOD-2));
        add(t,pcheck[A[i]],-exp(partial[A[i]],MOD-2));
    }


    for(ll i=0; i<N/2; i++){
        ll b = A[N-1-i];
        A[N-1-i] = A[i];
        A[i] = b;
    }

    for(ll i=0; i<N; i++){
        ll carry = sum(t2,A[i]);
        ll counter = sum(inv, A[i]-1);
        ans =  (ans + 2*counter - partial[A[i]]*carry)%MOD;
        add(t2,A[i]+1,exp(partial[A[i]],MOD-2));
        add(t2,pcheck[A[i]],-exp(partial[A[i]],MOD-2));
        add(inv, A[i], 1);

    }


    ans = (ans * exp(2,MOD-2))%MOD;

    for(ll i=1; i<N+1; i++){
        ans = (ans*freq[i])%MOD;
    }
    while(ans<0)
        ans+=MOD;
    cout<<ans<<endl;






    return 0;
}
