from collections import * from itertools import * from functools import * from heapq import * import sys,math # input = sys.stdin.readline N = int(input()) A = list(map(int,input().split())) mod = 10**9 + 7 C = Counter(A) ans = N*(N-1)*(N-2)//6 for v in C.values(): ans -= v*(v-1)//2*(N-v) + v*(v-1)*(v-2)//6 ans %= mod print(ans)