#include #define rep(i, a) rep2 (i, 0, a) #define rep2(i, a, b) for (int i = (a); i < (b); i++) #define repr(i, a) repr2 (i, 0, a) #define repr2(i, a, b) for (int i = (b) - 1; i >= (a); i--) #define chmin(a, b) ((b) < a && (a = (b), true)) #define chmax(a, b) (a < (b) && (a = (b), true)) #define rng(a) (a).begin(), (a).end() using namespace std; typedef long long ll; const ll mod = 1e9 + 7; ll dp[5050][5050]; ll fact[5050]; int main() { int N; cin >> N; vector a(N); rep (i, N) { int A; cin >> A; a[A]++; } fact[0] = 1; rep2 (i, 1, 5050) fact[i] = (fact[i - 1] * i) % mod; dp[0][0] = 1; dp[0][1] = a[0]; rep2 (i, 1, N) rep (j, i + 2) { if (j == 0) dp[i][j] = 1; else { dp[i][j] = (dp[i - 1][j] + dp[i - 1][j - 1] * a[i]) % mod; } } ll ans = 0; rep2 (i, 1, N + 1) { ll sign = i & 1 ? 1 : -1; ans += sign * dp[N - 1][i] * fact[N - i] % mod; } ans = (fact[N] - ans) % mod; ans += mod; ans %= mod; cout << ans << endl; return 0; }