// 基本 #include #include using namespace std; using namespace atcoder; // 型名の省略 using ll = long long; template using vc = vector; template using vvc = vector>; template using vvvc = vector>; template using vvvvc = vector>; template using vvvvvc = vector>; template using pq = priority_queue; template using pqg = priority_queue, greater>; // 定数 constexpr long long MOD = 1000000007LL; template constexpr T INF = 0; template <> constexpr int INF = 1001001001; template <> constexpr long long INF = 1LL << 61; template <> constexpr double INF = INF; template <> constexpr long double INF = INF; // 便利関数 // 入出力処理 template void input(T &...a) { (cin >> ... >> a); } void print() { cout << '\n'; } template void print(const T &a, const Ts &...b) { cout << a; (cout << ... << (cout << ' ', b)); cout << '\n'; } // 繰り返し処理 #define overload4(a, b, c, d, e, ...) e #define rep1(a) for (int i = 0; i < a; i++) #define rep2(i, a) for (int i = 0; i < a; i++) #define rep3(i, a, b) for (int i = a; i < b; i++) #define rep4(i, a, b, c) for (int i = a; i < b; i += c) #define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) // 配列処理 template bool operator==(const vector &a, const vector &b) { return (a.size() == b.size()) && equal(a.cbegin(), a.cend(), b.cbegin()); } template bool operator!=(const vector &a, const vector &b) { return !(a == b); } // 最小・最大処理 template inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template inline auto all_max(Args... args) { return max(initializer_list>{args...}); } template inline auto all_min(Args... args) { return min(initializer_list>{args...}); } // 追加ライブラリ using S = int; S op(S l, S r) { return l + r; } S e() { return 0; } /// ここからコードを書く int main() { cin.tie(0); int n; input(n); vvc a(n); rep(i, n - 1) { int at; input(at); a[at].push_back(i + 1); } segtree seg(n); ll ans = 0; auto dfs = [&](auto self, int cu) -> void { ans += seg.prod(0, cu); seg.set(cu, 1); for (int ne : a[cu]) { self(self, ne); } seg.set(cu, 0); }; dfs(dfs, 0); print(ans); return 0; }