#include #include #include #include //#include #include #include #include #include #include //#include #include #include #include //#include #include #include //#include #include #include #include const int dx[] = {1, 0, -1, 0}; const int dy[] = {0, 1, 0, -1}; using namespace std; typedef long long ll; typedef vector vi; typedef vector vll; typedef pair pii; typedef complex C; const int MAXN = 5005; const ll MOD = 1e9+7; int N; int cnt[MAXN]; ll dp[2][MAXN]; ll fact[MAXN]; int main() { cin.tie(0); ios::sync_with_stdio(false); fact[0] = 1; for (int i = 1; i < MAXN; i++) { fact[i] = (fact[i-1] * i) % MOD; } cin >> N; for (int i = 0; i < N; i++) { int a; cin >> a; cnt[a]++; } dp[0][0] = 1; for (int i = 0; i < N; i++) { int cur = i%2; int tar = (1^cur); memset(dp[tar], 0, sizeof(dp[tar])); for (int j = 0; j <= i; j++) { (dp[tar][j] += dp[cur][j]) %= MOD; (dp[tar][j+1] += dp[cur][j] * cnt[i]) %= MOD; } } ll ans = 0; for (int i = 0; i <= N; i++) { ll plus = (dp[N%2][i]*fact[N-i]) % MOD; if (i%2) plus *= -1; (ans += plus) %= MOD; } if (ans < 0) ans += MOD; cout << ans << endl; return 0; }