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())) conv = list(set(A)) conv.sort() inv = {a:i for i,a in enumerate(conv)} n = len(conv) class SegTree: """ Segment Tree """ def __init__(self, init_val, segfunc, ide_ele): """ 初期化 init_val: 配列の初期値 """ n = len(init_val) self.segfunc = segfunc self.ide_ele = ide_ele self.num = 1 << (n - 1).bit_length() self.tree = [ide_ele] * 2 * self.num # 配列の値を葉にセット for i in range(n): self.tree[self.num + i] = init_val[i] # 構築していく for i in range(self.num - 1, 0, -1): self.tree[i] = segfunc(self.tree[2 * i], self.tree[2 * i + 1]) def update(self, k, x): """ k番目の値をxに更新 k: index(0-index) x: update value """ k += self.num self.tree[k] = x while k > 1: self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1]) k >>= 1 def query(self, l, r): """ [l, r)のsegfuncしたものを得る l: index(0-index) r: index(0-index) """ res = self.ide_ele if r<1: return 0 if l>n-1: return 0 l += self.num r += self.num while l < r: if l & 1: res = self.segfunc(res, self.tree[l]) l += 1 if r & 1: res = self.segfunc(res, self.tree[r - 1]) l >>= 1 r >>= 1 return res X = SegTree([0]*n,lambda x,y:x+y,0) Y = SegTree([0]*n,lambda x,y:x+y,0) X.update(inv[A[0]],1) if N<=2: print(0) exit() ans = 0 for i in range(2,N): Y.update(inv[A[i]],Y.query(inv[A[i]],inv[A[i]]+1) + 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.update(inv[a],X.query(inv[a],inv[a]+1)+1) a = A[i+1] Y.update(inv[a],Y.query(inv[a],inv[a]+1)-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)