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++ (GCC 5.4.1) (judge verdict: Accepted)

#include<cstdio>
#include<cstring>
#include<cctype>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=200010,MOD=1000000007;
int read(){
    char c;int s=0,t=1;
    while(!isdigit(c=getchar()))if(c=='-')t=-1;
    do{s=s*10+c-'0';}while(isdigit(c=getchar()));
    return s*t;
}
struct cyc{
    int z0,z1,z2;
};
cyc operator + (cyc a,cyc b){
    cyc c;
    c.z0=1ll*a.z0*b.z0%MOD;
    c.z1=(1ll*a.z0*b.z1+1ll*a.z1*b.z0)%MOD;
    c.z2=(1ll*a.z0*b.z2+1ll*a.z2*b.z0+1ll*a.z1*b.z1+1ll*a.z2*b.z2+1ll*a.z1*b.z2+1ll*a.z2*b.z1)%MOD;
    return c;
}
vector<cyc>a[maxn];
int n,fa[maxn],first[maxn],cnt=0,tot=0,b[maxn];
struct edge{int v,from;}e[maxn*2];
void insert(int u,int v){cnt++;e[cnt].v=v;e[cnt].from=first[u];first[u]=cnt;}
int MO(int x){return x>=MOD?x-MOD:x;}
void merge(int &x,int y){
    if(a[x].size()<a[y].size())swap(x,y);
    for(int i=0;i<(int)a[y].size();i++){
        a[x][a[x].size()-i-1]=a[x][a[x].size()-i-1]+a[y][a[y].size()-i-1];
    }
}
int main(){
    n=read()+1;
    for(int i=2;i<=n;i++)fa[i]=read()+1,insert(fa[i],i);
    for(int i=n;i>=1;i--){
        int mx=0;
        if(!first[i]){
            a[b[i]=++tot].push_back((cyc){(MOD+1)/2,(MOD+1)/2,0});
        }
        else{
            b[i]=b[e[first[i]].v];
            for(int j=e[first[i]].from;j;j=e[j].from){
                mx=max(mx,min((int)a[b[i]].size(),(int)a[b[e[j].v]].size()));
                merge(b[i],b[e[j].v]);
            }
            a[b[i]].push_back((cyc){(MOD+1)/2,(MOD+1)/2,0});
        }
        for(int j=(int)a[b[i]].size()-1-1;j>=(int)a[b[i]].size()-mx-1;j--)a[b[i]][j].z0+=a[b[i]][j].z2,a[b[i]][j].z2=0;
    }
    int ans=0,N=1;
    for(int i=1;i<=n;i++)N=(N<<1)%MOD;
    for(int i=0;i<(int)a[b[1]].size();i++)ans=MO(ans+1ll*a[b[1]][i].z1*N%MOD);
    printf("%d",ans);
    return 0;
}