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: Time Limit Exceeded)

#include <iostream>
#include <cstdio>
#include <map>
#include <vector>
#include <algorithm>
#define PB push_back
#define MP make_pair
using namespace std;

typedef long long ll;
typedef pair<ll, ll> pii;
typedef vector<pii> vii;
typedef vector<vii> vvii;

const int MAX_N = 200001;
const ll MOD = 1000000007;

ll N, p;
ll l2[MAX_N], depth, max_depth = 0;

//map<ll, vector<ll> > tree;
vector<ll> tree[MAX_N];

void init() {
  cin >> N;
  for (int i=0; i<N; i++) {
    cin >> p;
    tree[p].PB(i + 1);
  }
  l2[0] = 1;
  for (int i=1; i<MAX_N; i++) {
    l2[i] = (l2[i-1] * 2) % MOD;
  }
}

ll inv(ll x) {
  ll res = 1, n = MOD - 2;
  while (0 < n) {
    if (n & 1) {
      res = x * res % MOD;
    }
    x = x * x % MOD;
    n >>= 1;
  }
  return res;
}

pair<vii, vector<ll> > dfs(ll x) {
  depth++;
  pair<vii, vector<ll> > res;
  vector<ll> nodes;
  ll a = tree[x].size();
  // cout << "50: tree[" << x << "].size() = " << a << endl;
  if (a == 0) {
    depth--;
    // cout << endl;
    return res;
  }
  ll idx, b = (l2[a] + MOD - a) % MOD;
  // cout << "57: x = " << x << ", b = " << b << endl;
  vvii xy; xy.PB(res.first);
  xy[0].PB(MP(a, b));

  nodes.PB(a);
  for (int i=0; i<tree[x].size(); i++) {
    idx = 1;
    pair<vii, vector<ll> > dfs_res = dfs(tree[x][i]);
    vii tmp = dfs_res.first;
    for (auto it=tmp.begin(); it!=tmp.end(); ++it) {
      if (xy.size() <= idx) xy.PB(res.first);
      xy[idx++].PB(*it);
    }

    vector<ll> child_cnts = dfs_res.second;
    for (int j=0; j<child_cnts.size(); j++) {
      if (nodes.size() <= j+1) nodes.PB(0);
      nodes[j+1] += child_cnts[j];
    }
  }

  // cout << "79: x = " << x << endl;
  for (int i=0; i<nodes.size(); i++) {
    // cout << "nodes[" << i << "] = " << nodes[i] << endl;
  }

  idx = 0;
  for (auto itr=xy.begin(); itr!=xy.end(); ++itr) {
    ll sum_ab = 0, seki_b = 1;
    // if (itr->size() == 0) continue;
    for (auto it=itr->begin(); it!=itr->end(); ++it) {
      // cout << "88: (a, b) = (" << it->first << ", " << it->second << ")" << endl;
      seki_b *= it->second; seki_b %= MOD;
      sum_ab += (it->first * inv(it->second)) % MOD;
      sum_ab %= MOD;
    }
    // printf("92: (sum_ab, seki_b) = (%lld, %lld)\n", sum_ab, seki_b);
    ll res_a = (seki_b * sum_ab) % MOD;
    ll res_b = (l2[nodes[idx++]] + MOD - res_a) % MOD;
    // printf("95: (res_a, res_b) = (%d, %d)\n", res_a, res_b);
    res.first.PB(MP(res_a, res_b));
  }
  depth--;
  res.second = nodes;
  // cout << endl;
  return res;
}

ll solve() {
  ll ans = l2[N];
  depth = -1;
  pair<vii, vector<ll> > tmp = dfs(0);
  vii res = tmp.first;
  int i=0;
  for (auto it=res.begin(); it!=res.end(); ++it) {
    // cout << "110: i:" << i << " (a, b) = (" << it->first << ", " << it->second << ")" << endl;
    // cout << "(x * y) = " << it->first << " * 2^" << N+1-tmp.second[i] << endl;
    ans += it->first * l2[N+1-tmp.second[i++]] % MOD;
    ans %= MOD;
  }

  return ans;
}

int main() {
  init();
  cout << solve() << endl;
  return 0;
}