import sys sys.setrecursionlimit(10 ** 9) class Bit: """1-indexed""" def __init__(self, n): self.size = n self.tree = [0] * (n + 1) def sum(self, i): s = 0 while i > 0: s += self.tree[i] i -= i & -i return s def add(self, i, x): while i <= self.size: self.tree[i] += x i += i & -i N = int(input()) A = map(int, input().split()) T = [[] for _ in range(N)] for i, a in enumerate(A, 1): T[a].append(i) bit = Bit(N) def dfs(v): res = bit.sum(v) bit.add(v + 1, 1) for x in T[v]: res += dfs(x) bit.add(v + 1, -1) return res print(dfs(0))