import typing import sys sys.setrecursionlimit(200050) class FenwickTree: '''Reference: https://en.wikipedia.org/wiki/Fenwick_tree''' def __init__(self, n: int = 0) -> None: self._n = n self.data = [0] * n def add(self, p: int, x: typing.Any) -> None: assert 0 <= p < self._n p += 1 while p <= self._n: self.data[p - 1] += x p += p & -p def sum(self, left: int, right: int) -> typing.Any: assert 0 <= left <= right <= self._n return self._sum(right) - self._sum(left) def _sum(self, r: int) -> typing.Any: s = 0 while r > 0: s += self.data[r - 1] r -= r & -r return s N = int(input()) A = list(map(int, input().split())) ft = FenwickTree(N) E = [[] for _ in range(N)] for i, a in enumerate(A, start=1): E[a].append(i) ans = 0 def dfs(x): global ans for y in E[x]: ans += ft.sum(0, y) ft.add(y, 1) dfs(y) ft.add(y, -1) ft.add(0, 1) dfs(0) print(ans)