#include #include #include using namespace std; template struct BIT { const Abel UNITY_SUM = 0; vector dat; BIT(int n) : dat(n, UNITY_SUM) { } // [0, n) inline void add(int i, Abel x) { while (i < dat.size()) { dat[i] += x; i |= i + 1; } } inline Abel sum(int i) { // [0, i] Abel res = UNITY_SUM; while (i >= 0) { res += dat[i]; i = (i & (i + 1)) - 1; } return res; } inline Abel sum(int a, int b) { return sum(b - 1) - sum(a - 1); } // [a, b) /* debug */ Abel operator[](int i) { return sum(i, i + 1); } void dump() { for (int i = 0; i < dat.size(); ++i) cout << sum(i, i + 1) << ","; cout << endl; } }; struct EulerTourOnVertex { vector posL, posR; vector> g; int pos; EulerTourOnVertex(int n) : posL(n), posR(n), g(n) { } void add_edge(int u, int v) { g[u].emplace_back(v); g[v].emplace_back(u); } void dfs(int u, int p) { posL[u] = pos++; for (int v: g[u]) if (v != p) { dfs(v, u); pos++; } posR[u] = pos; } void build(int r = 0) { pos = 0; dfs(r, -1); } }; int main() { int n; cin >> n; EulerTourOnVertex et(n); for (int u = 1; u < n; u++) { int p; cin >> p; et.add_edge(p, u); } et.build(); BIT bit(n * 2); long long cnt = 0; for (int i = n - 1; i >= 0; i--) { int left = et.posL[i]; int right = et.posR[i]; cnt += bit.sum(left, right + 1); bit.add(left, 1); } cout << cnt << endl; return 0; }