import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines import numpy as np N = int(readline()) A = np.array(read().split(),np.int64) cnt = np.bincount(A) two = cnt * (cnt-1) // 2 * (N-cnt) three = cnt * (cnt-1) * (cnt-2) // 6 x = N * (N-1) * (N-2) // 6 x -= (two + three).sum() MOD = 10**9 + 7 print(x % MOD)