from collections import Counter N = int(input()) A = list(map(int,input().split())) ans = N*(N-1)*(N-2)//6 cnt = Counter(A) def same3(n): return n*(n-1)*(n-2)//6 def same2(n): return n*(n-1)//2 def not_kadomatu(n, m): if m == 2: return same2(m)*n - m else: return same2(m)*(n-m) + same3(m) for count in cnt.values(): if count >= 2: nk = not_kadomatu(N,count) ans = ans - nk print(ans%1000000007)