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 (Clang 3.8.0) (judge verdict: Memory Limit Exceeded)

#include<deque>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<set>
#include<cmath>
#include<tuple>
#include<string>
#include<chrono>
#include<functional>
#include<iterator>
#include<random>
#include<unordered_set>
#include<unordered_map>
#include<array>
#include<map>
#include<bitset>
#include<iomanip>
using namespace std;
typedef long long int llint;
typedef long double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
//ios::sync_with_stdio(false);
//std::cin.tie(0);
//<< setprecision(20)
const int mod=1e9+7;
const llint big=1e15+100;
const long double pai=3.141592653589793238462643383279502884197;
const long double ena=2.71828182845904523536;
const long double eps=1e-7;
template <class T,class U>void mineq(T& a,U b){if(a>b){a=b;}}
template <class T,class U>void maxeq(T& a,U b){if(a<b){a=b;}}
template <class T> void soun(T& ar)
{sort(ar.begin(),ar.end());ar.erase(unique(ar.begin(),ar.end()),ar.end());}
llint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);}
llint lcm(llint a,llint b){return a/gcd(a,b)*b;}
int n;
vector<int>oya;
vector<vector<int>>ko;
vector<int>ran;
vector<int>zyu;
void solve(int ter){
	int j;
	for(auto it:ko[ter]){solve(it);}
	zyu.pub(ter);
}
int main(void){
	int i,j,n;cin>>n;
	oya.res(n+1);
	ko.res(n+1);
	ran.res(n+1);
	for(i=1;i<=n;i++){cin>>oya[i];ko[oya[i]].pub(i);}
	for(i=n;i>=0;i--){
		ran[i]++;
		maxeq(ran[oya[i]],ran[i]);
	}
	vector<deque<int>> aru(n+1);
	vector<deque<int>> nai(n+1);
	vector<vector<int>>kabu(n+1);
	solve(0);
	for(int h=0;h<=n;h++){
		int ter=zyu[h];//順序
		if(ko[ter].size()==0){
			aru[ter].puf(1);nai[ter].puf(1);
		}else{
			int sra=-1,sde=-1;
			for(auto it:ko[ter]){if(sra<=ran[it]){sde=it;sra=ran[it];}}
			aru[ter]=move(aru[sde]);
			nai[ter]=move(nai[sde]);
			for(auto it:ko[ter]){
				if(sde==it){continue;}
				deque<int>fir=move(aru[it]);
				deque<int>sec=move(nai[it]);
				if(kabu[ter].size()<fir.size()){kabu[ter].res(fir.size());}
				for(j=0;j<fir.size();j++){
					llint ar=(llint)aru[ter][j]*sec[j]+(llint)nai[ter][j]*(llint)fir[j];ar%=mod;
					llint na=(llint)nai[ter][j]*sec[j];na%=mod;
					llint ka=(llint)kabu[ter][j]*((llint)fir[j]+sec[j])+(llint)aru[ter][j]*(llint)fir[j];ka%=mod;
					aru[ter][j]=ar;nai[ter][j]=na;kabu[ter][j]=ka;
				}
			}
			for(j=0;j<kabu[ter].size();j++){nai[ter][j]+=kabu[ter][j];nai[ter][j]%=mod;}
			aru[ter].puf(1);nai[ter].puf(1);
		}
		kabu[ter].clear();
	}
	vector<int>fuka(n+1);
	vector<int>fkaz(n+1);fkaz[0]=1;
	vector<int>nzyo(n+2);nzyo[0]=1;
	for(i=1;i<=n+1;i++){nzyo[i]=(nzyo[i-1]*2)%mod;}
	for(i=1;i<=n;i++){fuka[i]=fuka[oya[i]]+1;fkaz[fuka[i]]++;}//逆深さ
	llint ans=0;
	for(j=0;j<aru[0].size();j++){
		ans+=(llint)nzyo[n+1-fkaz[j]]*aru[0][j];
		ans%=mod;
	}
	cout<<ans<<endl;
	return 0;
}