#include #define M_PI 3.14159265358979323846 // pi using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair P; typedef tuple t3; typedef tuple t4; #define rep(a,n) for(ll a = 0;a < n;a++) #define repi(a,b,n) for(ll a = b;a < n;a++) template void chmax(T& reference, T value) { reference = max(reference, value); } template void chmaxmap(map& m, T key, T value) { if (m.count(key)) { chmax(m[key], value); } else { m[key] = value; } } template void chmin(T& reference, T value) { reference = min(reference, value); } #include using namespace atcoder; typedef modint1000000007 mint; struct Point { ll x, y; bool operator <(const Point& p) const { return x < p.x || (x == p.x && y < p.y); } }; struct ConvexHullRunner { typedef ll coord_t; typedef ll coord2_t; coord2_t cross(const Point& O, const Point& A, const Point& B) { return (A.x - O.x) * (B.y - O.y) - (A.y - O.y) * (B.x - O.x); } vector convex_hull(vector P) { size_t n = P.size(), k = 0; if (n <= 3) return P; vector H(2 * n); sort(P.begin(), P.end()); for (size_t i = 0; i < n; ++i) { while (k >= 2 && cross(H[k - 2], H[k - 1], P[i]) <= 0) k--; H[k++] = P[i]; } for (size_t i = n - 1, t = k + 1; i > 0; --i) { while (k >= t && cross(H[k - 2], H[k - 1], P[i - 1]) <= 0) k--; H[k++] = P[i - 1]; } H.resize(k - 1); return H; } }; ll gcd(ll a, ll b) { while (b) a %= b, swap(a, b); return abs(a); } static ll raw_cmb(ll n, ll k) { ll c = 1; for (int i = 1; i <= k; i++) { c *= (n + 1 - i); c /= i; } return c; } struct BIT { typedef ll input_t; vector bit_; const long long n_; BIT(long long n) :n_(n) { bit_.resize(n + 1LL, 0); } input_t sum(int i) { input_t s = 0; while (i > 0) { s += bit_[i]; i -= i & (-i); } return s; } void add(int i, input_t x) { while (i <= n_) { bit_[i] += x; i += (i & (-i)); } } }; int main() { int n; cin >> n; vector> g(n + 1); for (int i = 1; i < n;i++) { ll p; cin >> p; g[p + 1].push_back(i + 1); } BIT bit(n); ll sum = 0; function dfs = [&](ll current, ll parent) { sum += bit.sum(current); bit.add(current, 1); for (auto x : g[current]) { if (x == parent) continue; dfs(x, current); } bit.add(current, -1); }; dfs(1, -1); cout << sum << endl; return 0; }