Language: C++

#include <stack>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

static const int N = 200010;
static const int MOD = 1000000007;
int n, d, tim, ans, nume, power[N], cnt[N], h[N], dfn[N], dep[N], id[N], up[N][20];
struct Edge {
	int v, nxt;
} e[N << 1];

bool cmpdep(int a, int b) {
	return dep[a] == dep[b] ? dfn[a] < dfn[b] : dep[a] < dep[b];
}

bool cmpdfn(int a, int b) { return dfn[a] < dfn[b]; }

void add_edge(int u, int v) {
	e[++ nume] = (Edge) { v, h[u] }, h[u] = nume;
	e[++ nume] = (Edge) { u, h[v] }, h[v] = nume;
}

void dfs(int t, int fa) {
	dfn[t] = tim ++, up[t][0] = fa;
	for (int i = h[t]; i; i = e[i].nxt)
		if (e[i].v != fa) dfs(e[i].v, t);
}

void init() {
	power[0] = 1;
	for (int i = 1; i <= n; ++ i) power[i] = (power[i - 1] << 1) % MOD;
	for (int i = 1; i <= n; ++ i) ++ cnt[dep[i]];
}

int get_lca(int x, int y) {
	if (dep[x] < dep[y]) swap(x, y);
	for (int i = 19; ~ i; -- i)
		if (dep[up[x][i]] >= dep[y]) x = up[x][i];
	if (x == y) return x;
	for (int i = 19; ~ i; -- i)
		if (up[x][i] != up[y][i]) x = up[x][i], y = up[y][i];
	return up[x][0];
}

vector <int> son[N];

void build(int l, int r) {
	vector <int> node; node.push_back(1);
	for (int i = l; i <= r; ++ i) node.push_back(id[i]);
	for (int i = node.size() - 1; i; -- i) node.push_back(get_lca(node[i - 1], node[i]));
	sort(node.begin(), node.end(), cmpdfn);
	int len = unique(node.begin(), node.end()) - node.begin();
	for (int i = 0; i < len; ++ i) son[i].clear();
	stack <int> st;
	for (int i = 0; i < len; ++ i)
		if (st.empty()) st.push(node[i]);
		else {
			int lca = get_lca(node[i], st.top());
			if (lca != st.top()) do {
				int t = st.top(); st.pop();
				if (! st.empty()) son[st.top()].push_back(t); else break;
			} while (lca != st.top());
			st.push(node[i]);
		}
	do {
		int t = st.top(); st.pop();
		if (! st.empty()) son[st.top()].push_back(t); else break;
	} while (1);
}

void ex_gcd(int a, int b, int &x, int &y) {
	if (! b) { x = 1, y = 0; return; }
	ex_gcd(b, a % b, y, x), y -= a / b * x;
}

int get_inv(int t) {
	int x, y; ex_gcd(t, MOD, x, y); return (x + MOD) % MOD;
}

pair <int, int> dp(int t) {
	if (! son[t].size()) return make_pair(1, 1);
	// int all = 1; vector <pair <int, int> > rec;
	// for (int i = 0; i < son[t].size(); ++ i) rec.push_back(dp(son[t][i])), all = 1ll * all * rec[i].first % MOD;
	dp(son[t][i]);
	pair <int, int> ret = make_pair(0, 0);
	// for (int i = 0; i < rec.size(); ++ i) (ret.second += 1ll * all * get_inv(rec[i].first) % MOD * rec[i].second % MOD) %= MOD;
	// all = 1;
	// for (int i = 0; i < rec.size(); ++ i) all = 1ll * all * (rec[i].first + rec[i].second) % MOD;
	// ret.first = (all - ret.second + MOD) % MOD;
	return ret;
}

void count(int l, int r) {
	build(l, r);
	pair <int, int> res = dp(1);
	(ans += 1ll * res.second * power[n - cnt[dep[id[l]]]] % MOD) %= MOD;
}

int main() {
	cin >> n, ++ n, id[1] = dep[1] = 1;
	for (int i = 2; i <= n + 1; ++ i) {
		int fa; cin >> fa, add_edge(fa + 1, i), d = max(d, dep[id[i] = i] = dep[fa + 1] + 1);
	}
	int p = 1; dfs(1, 0), sort(id + 1, id + n + 1, cmpdep), init();
	for (int i = 1; i < 20; ++ i)
		for (int j = 1; j <= n; ++ j) up[j][i] = up[up[j][i - 1]][i - 1];
	while (p <= n) {
		int q = p + 1;
		while (q <= n && dep[id[p]] == dep[id[q]]) ++ q;
		count(p, q - 1), p = q;
	}
	cout << ans << endl;
	return 0;
}


Compiler output:
a.cc: In function 'std::pair<int, int> dp(int)':
a.cc:87:19: error: 'i' was not declared in this scope
   87 |         dp(son[t][i]);
      |                   ^