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 "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "functional"
#include "ctime"

using namespace std;

//constexpr long long int MOD = 1000000007;
constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-8;

//int N, M, K, H, W, L, R;
long long int N, M, K, H, W, L, R;

long long int power(long long int x, long long int n, long long int M) {
	long long int ret = 1;
	long long int by = x;
	while (n) {
		if (n & 1) {
			ret *= by;
			ret %= M;
		}
		by *= by;
		by %= M;
		n >>= 1;
	}
	return ret;
}

void Calculate_Depth(vector<vector<int>>&edge, vector<int>&depth, int stnode) {
	depth[stnode] = 0;
	queue<int>Q;
	Q.push(stnode);
	while (!Q.empty()) {
		int cn = Q.front();
		Q.pop();
		for (auto i : edge[cn]) {
			if (depth[i] > depth[cn] + 1) {
				depth[i] = depth[cn] + 1;
				Q.push(i);
			}
		}
	}
	return;
}

class Lowest_Common_Ancestor {
	vector<int>depth;
	vector<vector<int>>parent;
	vector<vector<int>>edge;
	int height = 0;
	int node;
public:
	Lowest_Common_Ancestor(int num) {
		num++;
		node = num;
		while (num) {
			height++;
			num /= 2;
		}
		parent.resize(height);
		edge.resize(node);
		depth.resize(node);
		for (int i = 0; i < height; i++)parent[i].resize(node);
	}
	void Add_Edge(int a, int b) {
		edge[a].push_back(b);
		edge[b].push_back(a);
		return;
	}
	void Update(int rtnode) {
		queue<int>QQ;
		for (int i = 0; i < node; i++) depth[i] = INT_MAX;
		depth[rtnode] = 0;
		QQ.push(rtnode);
		while (!QQ.empty()) {
			int c = QQ.front();
			for (auto i : edge[c]) {
				if (depth[i] > depth[c] + 1) {
					depth[i] = depth[c] + 1;
					QQ.push(i);
				}
			}
			QQ.pop();
		}
		parent[0][rtnode] = -1;
		for (int i = 1; i < node; i++) {
			for (auto j : edge[i]) {
				if (depth[i] - 1 == depth[j]) {
					parent[0][i] = j;
					break;
				}
			}
		}
		for (int i = 0; i < height - 1; i++) {
			for (int j = 0; j < node; j++) {
				if (parent[i][j] < 0)parent[i + 1][j] = -1;
				else parent[i + 1][j] = parent[i][parent[i][j]];
			}
		}
		return;
	}
	int LCA(int u, int v) {
		if (depth[u] > depth[v])swap(u, v);
		for (int i = 0; i < height; i++) {
			if ((depth[v] - depth[u]) >> i & 1) {
				v = parent[i][v];
			}
		}
		if (u == v)return u;
		for (int i = height - 1; i >= 0; i--) {
			if (parent[i][v] != parent[i][u]) {
				u = parent[i][u];
				v = parent[i][v];
			}
		}
		return parent[0][u];
	}
	int Dist(int u, int v) {
		return depth[u] + depth[v] - depth[LCA(u, v)] * 2;
	}
};

void Search(const vector<vector<int>>&edge, vector<vector<int>>&node, int depth = 0, int num = 0) {
	node[depth].push_back(num);
	for (auto i : edge[num]) {
		Search(edge, node, depth + 1, i);
	}
}

int by[200001];

void Search(const vector<vector<int>>&edge, const vector<int>&node, const vector<int>&depth, unordered_map<long long int, int>&zero, unordered_map<long long int, int>&one, Lowest_Common_Ancestor &lca, int l, int r) {
	if (l == r) {
		one[1LL * l*MOD + r] = 1;
		zero[1LL * l*MOD + r] = 1;
		return;
	}
	int oone = 0;
	int zzero = 1;
	int ttwo = 0;
	int fl = l, fr = r;
	while (r >= l) {
		L = l;
		R = r + 1;
		int box = lca.LCA(node[fr], node[fl]);
		while (R - L > 1) {
			int mid = (R + L) / 2;
			if (box == lca.LCA(node[l], node[mid]))R = mid;
			else L = mid;
		}
		int nzero;
		int none;
		int ntwo;
		if (l == L) {
			nzero = zzero;
			none = (zzero + oone) % MOD;
			ntwo = ((oone + ttwo) % MOD + ttwo) % MOD;
		}
		else {
			Search(edge, node, depth, zero, one, lca, l, L);
			long long int index = 1LL * l*MOD + L;
			nzero = (1LL * zzero * zero[index]) % MOD;
			none = (1LL * zzero * one[index] + 1LL * oone * zero[index]) % MOD;
			ntwo = (1LL * oone*one[index] + 1LL * ttwo*(one[index] + zero[index])) % MOD;
			//ntwo = (1LL * ((oone + zzero) % MOD + ttwo)*(one[index] + zero[index])) % MOD;
			//ntwo %= MOD;
			//ntwo += MOD;
			//ntwo -= nzero;
			//ntwo %= MOD;
			//ntwo += MOD;
			//ntwo -= none;
			//ntwo %= MOD;
		}
		zzero = nzero;
		oone = none;
		ttwo = ntwo;
		l = R;
	}
	one[1LL * fl*MOD + fr] = oone;
	zero[1LL * fl*MOD + fr] = (zzero + ttwo) % MOD;
	return;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> N;
	N++;
	vector<int>p(N,-1);
	vector<vector<int>>edge(N);
	Lowest_Common_Ancestor lca(N);
	for (int i = 1; i < N; i++) {
		cin >> p[i];
		edge[p[i]].push_back(i);
		lca.Add_Edge(p[i], i);
	}
	by[0] = 1;
	for (int i = 1; i <= 200000; i++) {
		by[i] = by[i - 1] * 2;
		by[i] %= MOD;
	}
	lca.Update(0);
	vector<vector<int>>node(N);
	vector<int>depth(N, MOD);
	Calculate_Depth(edge, depth, 0);
	Search(edge, node);
	long long int ans = 0;
	for (int i = 0; i < N; i++) {
		if (node[i].empty())break;
		unordered_map<long long int, int>zero(N);
		unordered_map<long long int, int>one(N);
		Search(edge, node[i], depth, zero, one, lca, 0, (int)node[i].size() - 1);
		ans += one[node[i].size() - 1] * power(2, N - node[i].size(), MOD);
		ans %= MOD;
	}
	cout << ans << endl;
}