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 9.2.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
#pragma GCC optimize ("O2,unroll-loops")
//#pragma GCC optimize("no-stack-protector,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) {cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;}
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;

const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod=1000000007;
const int MAXN=200010, LOG=18;

int n, m, k, u, v, x, y, t, a, b;
ll tav[MAXN], dp0[MAXN], dp1[MAXN], ans;
int par[LOG][MAXN], sz[MAXN], A[MAXN*2], N;
int stt[MAXN], fnt[MAXN], h[MAXN], timer=1;
vector<int> G[MAXN], G2[MAXN], vec[MAXN];

void dfs(int node){
	stt[node]=timer++;
	for (int v:G[node]) dfs(v);
	fnt[node]=timer;
}

int Lca(int x, int y){
	if (stt[x]>stt[y]) swap(x, y);
	if (fnt[y]<=fnt[x]) return x;
	for (int i=LOG-1; ~i; i--) if (stt[x]<stt[par[i][y]]) y=par[i][y];
	return par[0][y];
}

inline bool is_par(int x, int y){
	return stt[x]<=stt[y] && fnt[y]<=fnt[x];
}
inline bool cmp(int x, int y){
	return stt[x]<stt[y];
}
inline ll powmod(ll a, ll b){
	ll res=1;
	for (; b; b>>=1, a=a*a%mod) if (b&1) res=res*a%mod;
	return res;
}
inline ll inv(ll a){
	return powmod(a, mod-2);
}

int main(){
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	tav[0]=1;
	for (int i=1; i<MAXN; i++) tav[i]=tav[i-1]*2%mod;
	cin>>n;
	for (int i=1; i<=n; i++){
		cin>>par[0][i];
		G[par[0][i]].pb(i);
		h[i]=h[par[0][i]]+1;
		vec[h[i]].pb(i);
	}
	for (int j=1; j<LOG; j++) for (int i=1; i<=n; i++) par[j][i]=par[j-1][par[j-1][i]];
	dfs(0);
	ans=tav[n];
	for (int i=1; vec[i].size(); i++){
		N=0;
		for (int j:vec[i]) A[N++]=j;
		sort(A, A+N, cmp);
		for (int j=N-1; j; j--) A[N++]=Lca(A[j], A[j-1]);
		sort(A, A+N);
		N=unique(A, A+N)-A;
		sort(A, A+N, cmp);
		
		stack<int> stk;
		stk.push(A[0]);
		for (int j=1; j<N; j++){
			while (!is_par(stk.top(), A[j])) stk.pop();
			G2[stk.top()].pb(A[j]);
			stk.push(A[j]);
		}
		for (int j=N-1; ~j; j--){
			int v=A[j];
			if (h[v]==i) dp0[v]=dp1[v]=sz[v]=1;
			else{
				sz[v]=dp1[v]=0;
				ll mul=1, t0=0;
				for (int u:G2[v]){
					sz[v]+=sz[u];
					if (dp0[u]) mul=mul*dp0[u]%mod;
					else t0++;
				}
				for (int u:G2[v]) if (t0-(dp0[u]==0)==0)
					dp1[v]=(dp1[v] + mul*inv(dp0[u])%mod*dp1[u])%mod;
				dp0[v]=(tav[sz[v]]-dp1[v])%mod;
			}
			G2[v].clear();
		}
		ans=(ans + dp1[A[0]]*tav[n+1-sz[A[0]]])%mod;
	}
	if (ans<0) ans+=mod;
	cout<<ans<<"\n";
	
	return 0;
}