#include using namespace std; struct iofast_t { iofast_t() { ios::sync_with_stdio(false); cin.tie(nullptr); } } iofast; struct uns_t {} uns; template auto vec(Element init, Head arg, Args ...args) { if constexpr (sizeof...(Args) == 0) return std::vector(arg, init); else return std::vector(arg, vec(init, args...)); } template auto vec(uns_t, Head arg, Args ...args) { return vec(Element(), arg, args...); } template > T &chmin(T &l, T r, Compare &&f = less()) { return l = min(l, r, f); } template > T &chmax(T &l, T r, Compare &&f = less()) { return l = max(l, r, f); } #include template struct segtree; template struct segtree : atcoder::segtree { using atcoder::segtree::segtree; }; int op(int x, int y) { return x + y; } int e() { return 0; } int main() { int n; cin >> n; auto g = vec(uns, n, 0); for (int i = 1; i < n; ++i) { int a; cin >> a; g[a].push_back(i); g[i].push_back(a); } int64_t ans = 0; segtree seg(n); auto dfs = [&](auto &&self, int v, int p = -1) -> void { ans += seg.prod(0, v); seg.set(v, 1); for (auto u : g[v]) { if (u == p) { continue; } self(self, u, v); } seg.set(v, 0); }; dfs(dfs, 0); cout << ans << endl; }