from collections import Counter MOD = 10 ** 9 + 7 N = int(input()) A = list(map(int, input().split())) C = Counter(A) NC = len(C) dp = [1, 0, 0, 0] for v in C.values(): dp2 = [1, 0, 0, 0] for i in range(3): dp2[i + 1] = dp[i + 1] + v * dp[i] dp2[i + 1] %= MOD dp = dp2 print(dp[3])