#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; #define rep(i, n) for(int i = 0; i < (n); i++) #define revrep(i, n) for(int i = (n)-1; i >= 0; i--) #define pb push_back #define f first #define s second void BinarySay(ll x, ll y = 60){rep(i, y) cout << (x>>(y-1-i) & 1); cout << endl;} const ll INFL = 1LL << 60;//10^18 = 2^60 int MOD = 1000000007; int kai[5010]; int N; vector A; int dp[2][5010]; void solve(){ kai[0] = 1; rep(i, 5009){ ll x = kai[i]; x *= (i+1); x %= MOD; kai[i+1] = (int)x; } sort(A.begin(), A.end()); dp[0][0] = 1; int s = 0; rep(i, 5005){ int cnt = 0; while(A[s] == i){ s++; cnt++; } rep(j, 5005){ dp[(i+1)&1][j] += dp[i&1][j]; dp[(i+1)&1][j] %= MOD; ll x = dp[i&1][j]; x *= cnt; x %= MOD; x += dp[(i+1)&1][j+1]; x %= MOD; dp[(i+1)&1][j+1] = (int)x; } rep(j, 5005){ dp[i&1][j] = 0; } } int ans = 0; rep(j, N){ if(j & 1){ ll x = ans; ll y = kai[N-j]; y *= dp[1][j]; y %= MOD; x = (x - y + MOD) % MOD; ans = (int)x; }else{ ll x = ans; ll y = kai[N-j]; y *= dp[1][j]; y %= MOD; x = (x + y) % MOD; ans = (int)x; } } cout << ans << endl; } int main(){ cin >> N; A.resize(N); rep(i, N){ cin >> A[i]; } solve(); }