#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;
}

### Prompt

Generate a cpp solution to the following problem:

Snuke has an integer sequence A whose length is N. He likes permutations of (1, 2, ..., N), P, that satisfy the following condition:

* P_i \leq A_i for all i ( 1 \leq i \leq N ).



Snuke is interested in the inversion numbers of such permutations. Find the sum of the inversion numbers over all permutations that satisfy the condition. Since this can be extremely large, compute the sum modulo 10^9+7.

Constraints

* 1 \leq N \leq 2 \times 10^5
* 1 \leq A_i \leq N ( 1 \leq i \leq N )
* All values in input are integers.

Input

Input is given from Standard Input in the following format:


N
A_1 A_2 ... A_N


Output

Print the sum of the inversion numbers over all permutations that satisfy the condition.

Examples

Input

3
2 3 3


Output

4


Input

6
4 2 5 1 6 3


Output

7


Input

5
4 4 4 4 4


Output

0


Input

30
22 30 15 20 10 29 11 29 28 11 26 10 18 28 22 5 29 16 24 24 27 10 21 30 29 19 28 27 18 23


Output

848414012

### Response

```cpp
#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;
}

```