推公式就不推了,学个这个公式就够了

#include<bits/stdc++.h>
#define PI acos(-1.0)
#define pb push_back
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=2000;
const int MOD=1e9+7;
template <class T>
bool sf(T &ret){ //Faster Input
    char c; int sgn; T bit=0.1;
    if(c=getchar(),c==EOF) return 0;
    while(c!='-'&&c!='.'&&(c<'0'||c>'9')) c=getchar();
    sgn=(c=='-')?-1:1;
    ret=(c=='-')?0:(c-'0');
    while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
    if(c==' '||c=='\n'){ ret*=sgn; return 1; }
    while(c=getchar(),c>='0'&&c<='9') ret+=(c-'0')*bit,bit/=10;
    ret*=sgn;
    return 1;
}
ll F[N],inv[N],Finv[N];
ll B[N];
ll comb(ll n,ll m){
    if(m<0||m>n)    return 0;
    return F[n]*1ll*Finv[n-m]%MOD*Finv[m]%MOD;
}
void init(){
    inv[1]=1;
    for(int i=2;i<N;i++)
        inv[i]=(MOD-MOD/i)*1ll*inv[MOD%i]%MOD;
    F[0]=Finv[0]=1;
    for(int i=1;i<N;i++){
        F[i]=F[i-1]*1ll*i%MOD;
        Finv[i]=Finv[i-1]*1ll*inv[i]%MOD;
    }
    B[0] = 1; /// 伯努利数
    for(int i=1; i<N; i++)
    {
        ll ans = 0;
        if(i == N - 1) break;
        for(int j=0; j<i; j++)
        {
            ans += comb(i+1,j) * B[j];
            ans %= MOD;
        }
        ans *= -inv[i+1];
        ans = (ans % MOD + MOD) % MOD;
        B[i] = ans;
    }
}
ll qm(ll a,ll b){
    ll ans=1%MOD;
    while(b){
        if(b&1) ans=ans*a%MOD;
        b>>=1;
        a=a*a%MOD;
    }
    return ans%MOD;
}
ll n,a[N];
ll gao(ll n,ll k){
    ll ans=0;
    for(int i=1;i<=k+1;i++){
        ans=(ans+ comb(k+1,i)%MOD*B[k+1-i]%MOD*qm(n+1,i)%MOD)%MOD;
    }
    ans=ans*inv[k+1]%MOD;
    return ans;
}
int main(void){
    init();
    while((scanf("%lld",&n))==1){
        for(int i=1;i<=n;++i)   sf(a[i]);
        sort(a+1,a+1+n);
        ll mul=1,ans=0;
        ans=a[n];
        for(int i=1;i<=n;i++)   ans*=a[i],ans%=MOD;
        for(int i=1;i<=n;i++){
            ll det;
            if(i!=n)    det=mul*(gao(a[i],n-i+1)-gao(a[i-1],n-i+1))%MOD;
            else det=mul*(gao(a[i]-1,n-i+1)-gao(a[i-1],n-i+1))%MOD;
            det+=MOD;
            det%=MOD;
            ans= ((ans-det)%MOD+MOD)%MOD;
            mul*=a[i];
            mul%=MOD;
        }
        printf("%lld\n",ans);
    }

    return 0;
}