#!/usr/bin/python from collections import Counter mul = lambda x, y: x*y def nCr(n, r): if r == 0 or n == r: return 1 k = r if r <= n-r else n-r return reduce(mul, xrange(n, n-k, -1)) / reduce(mul, xrange(1, k+1)) mod = int(1e9) + 7 n = int(raw_input()) As = map(int, raw_input().split()) res = nCr(n, 3) for v in filter(lambda e: e>1, Counter(As).values()): res -= nCr(v, 2) * (n-v) if v >= 3: res -= nCr(v, 3) print res % mod