def compress(As): '''とびとびの値のリストを連続する値のリストに変換する。 ''' val2idx = {a: i for i, a in enumerate(sorted(set(As)))} return [val2idx[a] for a in As] def add_count_f(val, data, size): '''セグメントツリーへの要素 val を追加し、 val 未満の要素の個数と、val 超の要素の個数を返す。 ''' pos = size + val count = 0 while pos: if pos & 1: count += data[pos-1] data[pos] += 1 pos >>= 1 return count, data[1] - count - data[size + val] def count_aba2(As): maxA = max(As) freq = [-1] * (maxA + 1) for a in As: freq[a] += 1 posList = {i:[] for i in xrange(maxA + 1) if freq[i]} for i, a in enumerate(As): if freq[a]: posList[a].append(i) count = 0 for poss in posList.values(): n = len(poss) for i in range(1, n): count += (poss[i] - poss[i-1] - 1) * i * (n-i) return count N = int(raw_input()) As = compress(list(map(int, raw_input().split()))) size = 1 << (len(bin(N)) - 2) segL = [0] * (size * 2) segR = [0] * (size * 2) belowL = [0] * N aboveL = [0] * N result = 0 for i, a in enumerate(As): below, above = add_count_f(a, segL, size) belowL[i] = below aboveL[i] = above for i, a in enumerate(As[::-1], 1): belowR, aboveR = add_count_f(a, segR, size) result += belowL[-i] * belowR + aboveL[-i] * aboveR # 左右が同じ値の疑似門松列の個数を差し引く result -= count_aba2(As) print(result)