MOD = 10**9 + 7 N = int(input()) A = [int(input()) for _ in range(N)] from collections import defaultdict freq = defaultdict(int) for a in A: freq[a] += 1 # Precompute factorials modulo MOD fact = [1] * (N + 1) for i in range(1, N + 1): fact[i] = fact[i-1] * i % MOD # Initialize DP array dp = [0] * (N + 2) # Extra space to avoid index issues dp[0] = 1 current_max_degree = 0 for x in freq: c = freq[x] # Iterate from current_max_degree down to 0 for k in range(current_max_degree, -1, -1): dp[k + 1] = (dp[k + 1] + dp[k] * c) % MOD current_max_degree += 1 # Calculate the answer ans = 0 for k in range(0, current_max_degree + 1): if k > N: continue sign = -1 if k % 2 else 1 term = (sign * dp[k] * fact[N - k]) % MOD ans = (ans + term) % MOD print(ans % MOD)