#include<bits/stdc++.h>
using namespace std;

//#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")


#define pb push_back
#define fi first
#define se second
#define ll long long
#define tp top()
#define fr front()
#define vi vector<int>
#define sz size()
#define rep(i,a,b) for(int i = a; i < b; ++i)
#define mem(a,  b) memset(a,  (b),  sizeof(a))
#define clr(a) memset(a,  0,  sizeof(a))
#define sqr(x) ( (x) * (x) )
#define all(v) v.begin(),  v.end()

typedef pair<int, int> pii;
typedef pair<int,pii> pip;
typedef pair<pii,int> ppi;
typedef pair<pii,pii> ppp;



void base(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}

#define N 200020
vector<int> adj[N];
vector<pii> qq[N];
int h[N], ans[N];

void dfs(int cur, int hh){	
	for(auto x : qq[cur]) ans[x.se] -= h[x.fi];	
	h[hh]++;
	for(int to : adj[cur]) dfs(to,hh+1);			
	for(auto x : qq[cur]) ans[x.se] += h[x.fi];	
}

void solve(){
	int n; cin>>n;
	rep(i,2,n+1){
		int u; cin>>u;
		adj[u].pb(i);
	}
	int q; cin>>q;
	rep(i,0,q){
		int u,d; cin>>u>>d;
		qq[u].pb({d,i});
	}
	dfs(1,0);
	rep(i,0,q){
		cout<<ans[i]<<"\n";
	}
}

int main()
{
	base();
//  freopen("input.txt", "r", stdin);
//  freopen("output.txt", "w", stdout);
	int t=1;
//	cin>>t;
	while(t--){
		solve();
	}
	return 0;
}





#include<bits/stdc++.h>
using namespace std;
const int N = 3100;
const int P = 998244353;
#define fr(i,a,b) for(int i = a;i <= b; ++i)
#define nfr(i,a,b) for(int i = a;i >= b; --i)

int n,k;
int f[N][N];
int vis[N][N];

inline void add(int &x,int y){
   x += y; if(x >= P)x -= P;
}

int sum[N*2];
inline void insert(int x,int y){
   if(x%2 == 1)return ;
   add(sum[x/2-y+N],f[x][y]);
}
inline int query(int x,int y){
   return sum[x-y+N];
}

int main(){
   scanf("%d%d",&k,&n);
   memset(f,0,sizeof f);

   f[0][0] = 1; insert(0,0);
   fr(i,1,k)
      nfr(j,i,0){
         f[j][i] = query(j,i);
         //cerr <<j <<' ' << i <<' ' << f[j][i] << endl;
         insert(j,i);
      }
   if(n > k)puts("0");
   else printf("%d\n",f[n][k]);
   return 0;
}
