from collections import defaultdict import sys sys.setrecursionlimit(10 ** 6) class FenwickTree: def __init__(self, n): self.data = [0] * n self.n = n def add(self, p, x): assert 0 <= p < self.n p += 1 while p < len(self.data): self.data[p] += x p += p & -p def sum(self, p): """区間 [0, p] の和""" assert 0 <= p < self.n p += 1 s = 0 while p > 0: s += self.data[p] p -= p & -p return s def rangesum(self, l, r): """区間 [l, r] の和""" assert 0 <= l <= r < self.n s = self.sum(r) if l > 0: s -= self.sum(l-1) return s class EulerTourVertex: def __init__(self, n, adj, root=0): vs = [] # 頂点 (訪問順) v_in = [0] * n # 頂点の最初に通った時刻 v_out = [0] * n # 頂点の最後に通った時刻 def dfs(v, par): vs.append(v) v_in[v] = len(vs)-1 for to in adj[v]: if to == par: continue dfs(to, v) vs.append(v) v_out[v] = len(vs)-1 dfs(root, -1) self.vs = vs self.v_in = v_in self.v_out = v_out N = int(input()) A = list(map(int, input().split())) adj = defaultdict(list) for i in range(N-1): par = A[i] adj[par].append(i+1) et = EulerTourVertex(N, adj, root=0) ft = FenwickTree(len(et.vs) + 10) ans = 0 for i in reversed(range(N)): # 頂点番号の大きい順 # 部分木に、頂点 i よりも大きい頂点の個数をカウント ans += ft.rangesum(et.v_in[i], et.v_out[i]) ft.add(et.v_in[i], 1) print(ans)