#include <bits/stdc++.h> using namespace std; typedef long long ll; const int MOD = 1000000007; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; // Count occurrences. // The Python code uses an array C of size 5000. const int MAXVAL = 5000; vector<int> C(MAXVAL, 0); for (int i = 0; i < N; i++){ int x; cin >> x; // Increase count for number x. if(x < MAXVAL) C[x]++; } // dp[k]: number of ways to choose k "NG" items. // Initialize dp[0] = 1. vector<ll> dp(N+1, 0); dp[0] = 1; // For each i from 0 to N-1, update dp in reverse order. // This corresponds to: // for i in range(N): // for j in range(N-1, -1, -1): // dp[j+1] += dp[j] * C[i] (mod MOD) for (int i = 0; i < N; i++){ for (int j = N - 1; j >= 0; j--){ dp[j+1] = (dp[j+1] + dp[j] * C[i]) % MOD; } } // Precompute factorials: fac[i] = i! mod MOD for i = 0..N. vector<ll> fac(N+1, 1); for (int i = 1; i <= N; i++){ fac[i] = (fac[i-1] * i) % MOD; } // Calculate the answer. // In the Python code, the term (i%2*(-2)+1) equals 1 if i is even and -1 if i is odd. // Thus, we sum for i = 0..N: dp[i] * fac[N-i] * ( (i even) ? 1 : -1 ). ll ans = 0; for (int i = 0; i <= N; i++){ ll term = (dp[i] * fac[N - i]) % MOD; if(i % 2 == 1) { // if odd, subtract the term (multiplying by MOD-1 gives -1 modulo MOD) term = (term * (MOD - 1)) % MOD; } ans = (ans + term) % MOD; } cout << ans % MOD << "\n"; return 0; }