#include using namespace std; #define REP(i,n) for(int i=0; i<(int)(n); i++) template class BIT { vector bit; public: BIT() {} // manage data in [1, n] BIT(int n) : bit(n + 1) {} void init() { fill(bit.begin(), bit.end(), 0); } // return sum in [1, i] T sum(int i){ T s = 0; while(i > 0){ s += bit[i]; i -= i & -i; } return s; } // return sum in [l, r] T sum(int l, int r) { return sum(r) - sum(l-1); } void add(int i, T x){ while(i < (int)bit.size()){ bit[i] += x; i += i & -i; } } }; int n; vector edges[200000+1]; long long dfs(int v, BIT &bit) { long long ret = bit.sum(v); bit.add(v, 1); for (int w: edges[v]) { ret += dfs(w, bit); } bit.add(v, -1); return ret; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i < n; i++) { int par; cin >> par; edges[par+1].push_back(i+1); } BIT bit(n); cout << dfs(1, bit) << endl; return 0; }