from collections import Counter N = int(input()) A = list(map(int,input().split())) MOD = 10 ** 9 + 7 fact = [1]*(N+1) rfact = [1]*(N+1) r = 1 for i in range(1, N+1): fact[i] = r = r * i % MOD rfact[N] = r = pow(fact[N], MOD-2, MOD) for i in range(N, 0, -1): rfact[i-1] = r = r * i % MOD def comb(n, k): return fact[n] * rfact[k] * rfact[n-k] % MOD C = Counter(A) NC = len(C) if NC < 3: print(0) exit() ans = comb(NC,3) for v in C.values(): ans *= v ans %= MOD print(ans)