Problem:
Score : 1000 points

Problem Statement

Snuke has a rooted tree with N+1 vertices.
The vertices are numbered 0 through N, and Vertex 0 is the root of the tree.
The parent of Vertex i (1 \leq i \leq N) is Vertex p_i.

Besides this tree, Snuke also has an box which is initially empty and many marbles, and playing with them.
The play begins with placing one marble on some of the vertices, then proceeds as follows:

If there is a marble on Vertex 0, move the marble into the box.

Move each marble from the vertex to its parent (all at once).

For each vertex occupied by two or more marbles, remove all the marbles from the vertex.

If there exists a vertex with some marbles, go to Step 1. Otherwise, end the play.

There are 2^{N+1} ways to place marbles on some of the vertices.
For each of them, find the number of marbles that will be in the box at the end of the play, and compute the sum of all those numbers modulo 1,000,000,007.

Constraints

1 \leq N < 2 \times 10^{5}

0 \leq p_i < i

Partial Scores

In the test set worth 400 points, N < 2{,}000.

Input

Input is given from Standard Input in the following format:

N
p_1 p_2 ... p_{N}

Output

Print the answer.

Sample Input 1

2
0 0

Sample Output 1

8

When we place a marble on both Vertex 1 and 2, there will be multiple marbles on Vertex 0 by step 2. In such a case, these marbles will be removed instead of being moved to the box.

Sample Input 2

5
0 1 1 0 4

Sample Output 2

96

Sample Input 3

31
0 1 0 2 4 0 4 1 6 4 3 9 7 3 7 2 15 6 12 10 12 16 5 3 20 1 25 20 23 24 23

Sample Output 3

730395550

Be sure to compute the sum modulo 1,000,000,007.

Solution in C++14 (GCC 5.4.1) (judge verdict: Runtime Error)

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
#define fst first
#define snd second

/* clang-format off */
template <class T, size_t D> struct _vec { using type = vector<typename _vec<T, D - 1>::type>; };
template <class T> struct _vec<T, 0> { using type = T; };
template <class T, size_t D> using vec = typename _vec<T, D>::type;
template <class T> vector<T> make_v(size_t size, const T& init) { return vector<T>(size, init); }
template <class... Ts> auto make_v(size_t size, Ts... rest) { return vector<decltype(make_v(rest...))>(size, make_v(rest...)); }
template <class T> inline void chmin(T &a, const T& b) { if (b < a) a = b; }
template <class T> inline void chmax(T &a, const T& b) { if (b > a) a = b; }
/* clang-format on */

const ll MOD = 1e9 + 7;

ll power(ll x, ll e) {
  ll v = 1;
  for (; e > 0; e >>= 1) {
    if (e & 1) {
      v = v * x % MOD;
    }
    x = x * x % MOD;
  }
  return v;
}

ll inverse(ll x) {
  return power(x, MOD - 2);
}

const int LOG_N = 40;

int main() {
#ifdef DEBUG
  ifstream cin("in.txt");
#endif
  int N;
  while (cin >> N) {
    ++N;
    vec<int, 2> G(N), rG(N);
    for (int i = 1; i < N; i++) {
      int p;
      cin >> p;
      G[p].push_back(i);
      rG[i].push_back(p);
    }
    vec<int, 2> pre = make_v(N, LOG_N, -1);
    for (int i = 1; i < N; i++) pre[i][0] = rG[i][0];
    for (int j = 1; j < LOG_N; j++)
      for (int i = 0; i < N; i++)
        pre[i][j] = pre[i][j - 1] < 0 ? -1 : pre[pre[i][j - 1]][j - 1];
    vector<int> ds(N), lim(N, -1);
    auto dfs = [&](auto&& f, int v, int d) -> vector<int> {
      ds[v] = d;
      if (G[v].empty()) return {d};
      vector<int> top2;
      for (int u : G[v]) {
        auto ch = f(f, u, d + 1);
        top2.insert(top2.end(), ch.begin(), ch.end());
      }
      if (top2.size() >= 2) {
        sort(top2.rbegin(), top2.rend());
        top2.resize(2);
        lim[v] = top2[1];
      }
      return top2;
    };
    dfs(dfs, 0, 0);
    vec<ll, 3> dp(N);
    for (int i = 0; i < N; i++) {
      if (lim[i] != -1) {
        dp[i].resize(lim[i] - ds[i] + 1);
      } else {
        dp[i].resize(1);
      }
    }
    ll res = 0;
    auto getp = [&](int v, int d) {
      for (int j = LOG_N - 1; j >= 0; j--) {
        if (pre[v][j] < 0) continue;
        if (lim[pre[v][j]] < d) {
          v = pre[v][j];
        }
      }
      if (v >= 0) v = pre[v][0];
      return lim[v] >= d ? v : -1;
    };
    auto dfs2 = [&](auto&& f, int v) -> void {
      for (int u : G[v]) f(f, u);
      int lb = ds[v];
      int ub = lim[v] != -1 ? lim[v] : ds[v];
      dp[v][0].push_back(inverse(2));
      for (int d = lb; d <= ub; d++) {
        auto& xs = dp[v][d - lb];
        ll pr = 1;
        for (ll x : xs) pr = pr * (1 - x + MOD) % MOD;
        ll sum = 0;
        for (ll x : xs) {
          sum += pr * inverse(1 - x + MOD) % MOD * x % MOD;
          sum %= MOD;
        }
        int p = getp(v, d);
        if (p < 0) {
          res += sum;
          res %= MOD;
        } else {
          dp[p][d - ds[p]].push_back(sum);
        }
      }
    };
    dfs2(dfs2, 0);
    res = res * power(2, N) % MOD;
    cout << res << endl;
  }
  return 0;
}