class Compression: def __init__(self, iterable): self.vs = sorted(set(iterable)) self.v2i = {} for i, val in enumerate(self.vs): self.v2i[val] = i def __len__(self): return len(self.vs) def index(self, val): """val のインデックスを返す""" return self.v2i[val] def value(self, index): """インデックスに対応する値を返す""" return self.vs[index] def map(self, iterable): return [self.index(x) for x in iterable] class FenwickTree: def __init__(self, n: int): self.data = [0] * (n+10) self.n = (n+10) def add(self, p: int, x: int): assert 0 <= p < self.n p += 1 while p < len(self.data): self.data[p] += x p += p & -p def sum(self, p: int) -> int: """区間 [0, p] の和""" assert 0 <= p < self.n p += 1 s = 0 while p > 0: s += self.data[p] p -= p & -p return s def range_sum(self, l: int, r: int) -> int: """区間 [l, r] の和""" assert 0 <= l <= r < self.n s = self.sum(r) if l > 0: s -= self.sum(l-1) return s def get(self, p) -> int: return self.range_sum(p, p) # 真ん中を固定 def solve(): comp = Compression(A + [-INF, INF]) xs = [comp.index(a) for a in A] sz = len(comp) lt = FenwickTree(sz) rt = FenwickTree(sz) dup = FenwickTree(sz) # dup.get(x) : x の重複数 for x in xs: rt.add(x, 1) res = 0 for x in xs: # 真ん中を x で固定 # 真ん中を最小値とする l = lt.range_sum(x+1, sz) r = rt.range_sum(x+1, sz) res += l * r # 重複あり res -= dup.range_sum(x+1, sz) # 重複分を削除 # 真ん中を最大値とする l = lt.range_sum(0, x-1) r = rt.range_sum(0, x-1) res += l * r # 重複あり res -= dup.range_sum(0, x-1) # 重複分を削除 lt.add(x, 1) rt.add(x, -1) old = dup.get(x) dup.add(x, lt.get(x) * rt.get(x) - old) return res INF = 1 << 60 N = int(input()) A = list(map(int, input().split())) ans = solve() print(ans)