from collections import defaultdict import sys sys.setrecursionlimit(10 ** 6) class FenwickTree: def __init__(self, n): self.data = [0] * (n + 10) def add(self, p, x): assert 0 <= p p += 1 while p < len(self.data): self.data[p] += x p += p & -p def sum(self, p): """区間 [0, p] の和""" assert 0 <= p 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 s = self.sum(r) if l > 0: s -= self.sum(l-1) return s class EulerTour: def __init__(self, n: int, root: int, adj): tour = [] # オイラーツアー lt = [0] * n # 各頂点番号に対する tour の最左/最右の位置 rt = [0] * n def dfs(v, prev): lt[v] = len(tour) tour.append(v) for to in adj[v]: if to == prev: continue dfs(to, v) tour.append(v) rt[v] = len(tour) - 1 dfs(root, -1) self.tour = tour self.lt = lt self.rt = rt 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) ans = 0 et = EulerTour(N, 0, adj) ft = FenwickTree(len(et.tour) + 10) for i in range(N-1, -1, -1): ans += ft.rangesum(et.lt[i], et.rt[i]) ft.add(et.lt[i], 1) print(ans)