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 <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;

ll n,m,q,T,k;
const ll big = 1000000007ll;

vector<vector<ll> > C(200001, vector<ll>());

ll DP[2001][2001][2] = {0};
bool DPC[2001][2001][2] = {0};
ll TWO[200001] = {0};
ll D[200001] = {0};

ll upp(ll i, ll j){
    if(j == 0)return 1;
    if(j%2 == 0){
        ll h = upp(i,j/2);
        return (h*h)%big;
    }
    return (i*upp(i,j-1))%big;
}

ll dp(ll i, ll d, ll flag, ll par){
    if(d == 0)return 1;
    if(DPC[i][d][flag])return DP[i][d][flag];
    ll ans = 1;
    ll n0 = 1;
    ll tot = 1;
    ll dumb = -1;

    for(ll c1 = 0; c1 < sz(C[i]); c1++){
        ll a = C[i][c1];
        if(a != par){
            tot *= (dp(a,d-1,0,i) + dp(a,d-1,1,i));
            tot %= big;
            n0 *= dp(a,d-1,0,i);
            n0 %= big;
            if(dp(a,d-1,0,i) == 0){
                dumb = c1;
            }
        }
    }

    if(i == 0 && d == 1){

        //cout <<" tot: " <<  tot << "  n0: " << n0 << "  dumb: " << dumb << "\n";

    }

    if(dumb != -1){

        for(ll c1 = 0; c1 < sz(C[i]); c1++){
            ll a = C[i][c1];
            if(a != par){
                if(a == dumb){
                    ans *= dp(a,d-1,1,i);
                }
                else{
                    ans *= dp(a,d-1,0,i);
                }
                ans %= big;
            }
        }

    }
    else{
        ans = 0;
        for(ll c1 = 0; c1 < sz(C[i]); c1++){
            ll a = C[i][c1];
            if(a != par){
                ll thing = (n0*upp(dp(a,d-1,0,i),big-2))%big;
                thing *= dp(a,d-1,1,i);
                ans += thing;
                ans %= big;
            }
        }

    }

    if(flag == 0){
        ans = (tot+4*big-ans)%big;
    }
    DPC[i][d][flag] = 1;
    DP[i][d][flag] = ans;
    return ans;
}

void dfs(ll i,ll d, ll par){
    D[d]++;
    for(ll c1 = 0; c1 < sz(C[i]); c1++){
        ll a = C[i][c1];
        if(a != par){
            dfs(a,d+1,i);
        }
    }
}

int main() {
    ll a,b,c,d;


    ll t = 1;
    for(ll c1 = 0; c1 < 200001; c1++){
        TWO[c1] = t;
        t *= 2;
        t %= big;
    }

    cin >> n;
    for(ll c1 = 0; c1 < n; c1++){
        cin >> a;
        C[a].push_back(c1+1);
        C[c1+1].push_back(a);
    }
    dfs(0,0,-1);
    ll ans = 0;
    for(ll c1 = 0; c1 < n+1; c1++){
        if(D[c1] == 0)break;
        ans += TWO[n+1-D[c1]] * dp(0,c1,1,-1);
        ans %= big;
    }
    cout << ans << "\n";
    return 0;
}