Language: C++

#include<stdio.h>
#include<cstring>
#include<algorithm>
#include<math.h>
#define re register int
#define mod 1000000007
using namespace std;
typedef long long ll;
int read() {
	re x=0,f=1;
	char ch=getchar();
	while(ch<'0' || ch>'9') {
		if(ch=='-')	f=-1;
		ch=getchar();
	}
	while(ch>='0' && ch<='9') {
		x=10*x+ch-'0';
		ch=getchar();
	}
	return x*f;
}
inline int add(int x,int y) {
	return x+y>=mod?x+y-mod:x+y;
}
inline int del(int x,int y) {
	return x<y?x-y+mod:x-y;
}
int fpow(int b,int p) {
	int ans=1;
	while(p) {
		if(p&1)	ans=(ll)ans*b%mod;
		b=(ll)b*b%mod;
		p>>=1;
	}
	return ans;
}
const int Size=300005;
const int INF=0x3f3f3f3f;
int n,num,id[Size],d[Size],deep[Size],tot[Size];
struct node {
	int zero;
	int one;
};
vector<node> dp[Size];
int cnt,head[Size];
struct Edge {
	int v,next;
} w[Size<<1];
void AddEdge(int u,int v) {
	w[++cnt].v=v;
	w[cnt].next=head[u];
	head[u]=cnt;
}
int All[Size],Zero[Size],One[Size];
void dfs(int x) {
	int son=n+1;
	for(int i=head[x]; i; i=w[i].next) {
		int nxt=w[i].v;
		dfs(nxt);
		if(d[nxt]>d[son]) {
			son=nxt;
		}
	}
	if(son>n) {
		id[x]=++num;
	} else {
		id[x]=id[son];		//继承 
		d[x]=d[son]+1;
	}
	dp[id[x]].push_back((node){1,1});	//这一深度的dp 
	for(re i=0; i<=d[x]; i++) {
		All[i]=Zero[i]=1;
		One[i]=0;
	}
	for(re i=head[x]; i; i=w[i].next) {
		int nxt=w[i].v,sd=d[son]-d[nxt];
		for(re j=0; j<=d[nxt]; j++) {
			int nowd=sd+j;
			node now=dp[id[nxt]][j];
			All[nowd]=(ll)All[nowd]*(now.one+now.zero)%mod;
			Zero[nowd]=(ll)Zero[nowd]*now.zero%mod;
		}
	}
	for(re i=head[x]; i; i=w[i].next) {
		int nxt=w[i].v,sd=d[son]-d[nxt];
		for(re j=0; j<=d[nxt]; j++) {
			int nowd=sd+j;
			node now=dp[id[nxt]][j];
			One[nowd]=add(One[nowd],(ll)Zero[nowd]*fpow(now.zero,mod-2)%mod*now.one%mod);
		}
	}
	for(re i=0; i<d[x]; i++) {
		dp[id[x]][i].zero=del(All[i],One[i]);
		dp[id[x]][i].one=One[i];
	}
}
int main() {
	n=read();
	for(re i=1; i<=n; i++) {
		int fa=read();
		AddEdge(fa,i);
		deep[i]=deep[fa]+1;
		tot[deep[i]]++;
	}
	tot[0]++;
	d[n+1]=-INF;
	dfs(0);
	int ans=0;
	for(re i=0; i<=d[0]; i++) {
		node now=dp[id[0]][i];
		ans=add(ans,(ll)now.one*fpow(2,n+1-tot[d[0]-i])%mod);
	}
	printf("%d",ans);
	return 0;
}

Compiler output:
a.cc: In function 'int read()':
a.cc:10:12: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   10 |         re x=0,f=1;
      |            ^
a.cc:10:16: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   10 |         re x=0,f=1;
      |                ^
a.cc: At global scope:
a.cc:44:1: error: 'vector' does not name a type
   44 | vector<node> dp[Size];
      | ^~~~~~
a.cc: In function 'void dfs(int)':
a.cc:70:9: error: 'dp' was not declared in this scope; did you mean 'd'?
   70 |         dp[id[x]].push_back((node){1,1});       //这一深度的dp
      |         ^~
      |         d
a.cc:71:16: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   71 |         for(re i=0; i<=d[x]; i++) {
      |                ^
a.cc:75:16: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   75 |         for(re i=head[x]; i; i=w[i].next) {
      |                ^
a.cc:77:24: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   77 |                 for(re j=0; j<=d[nxt]; j++) {
      |                        ^
a.cc:84:16: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   84 |         for(re i=head[x]; i; i=w[i].next) {
      |                ^
a.cc:86:24: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   86 |                 for(re j=0; j<=d[nxt]; j++) {
      |                        ^
a.cc:92:16: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   92 |         for(re i=0; i<d[x]; i++) {
      |                ^
a.cc: In function 'int main()':
a.cc:99:16: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   99 |         for(re i=1; i<=n; i++) {
      |                ^
a.cc:109:16: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
  109 |         for(re i=0; i<=d[0]; i++) {
      |                ^
a.cc:110:26: error: 'dp' was not declared in this scope; did you mean 'd'?
  110 |                 node now=dp[id[0]][i];
      |                          ^~
      |                          d