import sys, math input = lambda: sys.stdin.readline()[:-1] def MI(): return map(int, input().split()) inf = 10**18 mod = 10**9+7 from itertools import * def lcom(s): grouped = groupby(s) res = [] for k, v in grouped: res.append((k, int(len(list(v))))) return res n = int(input()) a = sorted(MI()) b = lcom(a) b1, b3 = 0, 0 for _, i in b: b3 += i b3 -= b[0][1] ans = 0 for b2 in range(1, len(b)): b1 += b[b2-1][1] b3 -= b[b2][1] ans += b[b2][1] * b1 * b3 % mod ans %= mod print(ans%mod)