import sequtils,strutils type binaryIndexTree[I:static[int]] = array[I + 1,int] proc add(BIT :var binaryIndexTree,i : int, a : int)= var index = i while BIT[0] >= index: BIT[index] += a index += ((index xor (index - 1)) and index) proc sum(BIT : binaryIndexTree, i : int):int = var index = i while index > 0: result += BIT[index] index = (index and (index - 1)) var BIT : binaryIndexTree[200010] BIT[0] = 200010 var N = stdin.readline.parseInt A = stdin.readline.split.map(parseInt) B = newSeqWith(N + 1,newSeq[int](0)) ans = 0 for i,a in A: B[a + 1].add(i + 2) proc dfs(i : int) = ans += BIT.sum(i) BIT.add(i, 1) for p in B[i]: dfs(p) BIT.add(i, -1) dfs(1) echo ans