#ifndef LOCAL #include using namespace std; #define debug(...) (void(0)) #else #include "algo/debug.h" #endif #include using S = int64_t; S op(S a, S b) { return a + b; } S e() { return 0; } namespace std { template class y_combinator_result { Fun fun_; public: template explicit y_combinator_result(T &&fun) : fun_(std::forward(fun)) {} template decltype(auto) operator()(Args &&...args) { return fun_(std::ref(*this), std::forward(args)...); } }; template decltype(auto) y_combinator(Fun &&fun) { return y_combinator_result>(std::forward(fun)); } } // namespace std void solve() { int N; cin >> N; vector> G(N); for(int i = 1; i < N; i++) { int a; cin >> a; G[a].push_back(i); G[i].push_back(a); } atcoder::segtree seg(N); S ans = 0; y_combinator([&](auto self, int v, int p = -1) -> void { seg.set(v, 1); for(const auto&to: G[v]) if(to != p) { self(to, v); } ans += seg.prod(0, v); seg.set(v, 0); })(0); cout << ans << endl; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int tt = 1; // std::cin >> tt; while(tt--) { solve(); } }