from collections import * from itertools import * from functools import * from heapq import * import sys,math # input = sys.stdin.readline class FenwickTree(): def __init__(self, n): self.n = n self.data = [0] * n def build(self, arr): #assert len(arr) <= n for i, a in enumerate(arr): self.data[i] = a for i in range(1, self.n + 1): if i + (i & -i) <= self.n: self.data[i + (i & -i) - 1] += self.data[i - 1] def add(self, p, x): #assert 0 <= p < self.n p += 1 while p <= self.n: self.data[p - 1] += x p += p & -p def sum(self, r): #assert 0 <= r <= self.n s = 0 while r: s += self.data[r - 1] r -= r & -r return s def query(self, l, r): #assert 0 <= l <= r <= self.n return self.sum(r) - self.sum(l) N = int(input()) A = list(map(int,input().split())) conv = list(set(A)) conv.sort() inv = {a:i for i,a in enumerate(conv)} n = len(conv) X = FenwickTree(n) Y = FenwickTree(n) X.add(inv[A[0]],1) if N<=2: print(0) exit() ans = 0 for i in range(2,N): Y.add(inv[A[i]], 1) for i in range(1,N-1): a = A[i] ans += X.query(0,inv[a])*Y.query(0,inv[a]) + (X.query(inv[a]+1,n))*(Y.query(inv[a]+1,n)) X.add(inv[a],1) a = A[i+1] Y.add(inv[a],-1) Z = defaultdict(lambda:[]) for i,a in enumerate(A): Z[a].append(i) for v in Z.values(): k = len(v) for j in range(k-1): ans -= (v[j+1]-v[j]-1)*(j+1)*(k-1-j) print(ans)