MOD = 10**9 + 7 N = int(input()) A = [int(input()) for _ in range(N)] from collections import defaultdict # Count the occurrences of each disliked number cnt = defaultdict(int) for a in A: cnt[a] += 1 # DP to compute C_k: number of ways to choose k elements with all distinct A_i dp = [0] * (N + 1) dp[0] = 1 for v in cnt: c = cnt[v] # Iterate backwards to avoid overwriting the values we need to use for j in range(N, -1, -1): if dp[j]: if j + 1 <= N: dp[j + 1] = (dp[j + 1] + dp[j] * c) % MOD # Precompute factorials modulo MOD fact = [1] * (N + 1) for i in range(1, N + 1): fact[i] = fact[i - 1] * i % MOD # Calculate the answer using inclusion-exclusion principle ans = 0 for k in range(0, N + 1): term = pow(-1, k, MOD) * dp[k] % MOD term = term * fact[N - k] % MOD ans = (ans + term) % MOD print(ans % MOD)