#include #define REP(i, s, n) for (int i = s; i < n; i++) #define ALL(a) a.begin(), a.end() #define MOD 1000000007 using namespace std; typedef long long ll; template class BIT { public: BIT(int n) : N(n), A(n + 1, 0) {} T sum(int i) { T res = 0; while (i > 0) { res += A[i]; i -= i & (-i); } return res; } T sum(int i, int j) { return sum(j) - sum(i - 1); } void add(int i, T x) { while (i <= N) { A[i] += x; i += (i & (-i)); } } private: vector A; int N; }; ll dfs(BIT &B, vector> &G, int n) { // cout << "# looking at : " << n << endl; B.add(n + 1, 1); int ret = 0; for (auto g : G[n]) { ret += B.sum(g + 1); ret += dfs(B, G, g); } B.add(n + 1, -1); return ret; } int main() { int N; cin >> N; vector> G(N); REP(i, 1, N) { int a; cin >> a; G[a].push_back(i); } BIT bit(N); cout << dfs(bit, G, 0) << endl; return 0; }