from collections import Counter mod = 10 ** 9 + 7 N = int(input()) A = list(map(int, input().split())) counter = Counter(A) ans = N * (N - 1) * (N - 2) // 6 for v in counter.values(): ans -= v * (v - 1) // 2 * (N - v) + v * (v - 1) * (v - 2) // 6 ans %= mod print(ans)