#!/usr/bin/python from fractions import Fraction from collections import Counter mul = lambda x, y: x*y nCr = lambda n, r: int(reduce(mul, (Fraction(n-i, i+1) for i in xrange(r)), 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