#include <bits/extc++.h>
using namespace std;
using ll = long long;
#define REP(i,n) for(int i=0;i<int(n);i++)
#define FOR(i,a,b) for(int i=a;i<=int(b);i++)
#define ALL(x) x.begin(),x.end()
#define MOD (ll)1000000007


vector<vector<int>> pass;
vector<vector<int>> tour;
vector<pair<int, int>>inout;
int depth = 0;
int pcount = 0;
void dfs(int now){
  inout[now].first = pcount;
  tour[depth].emplace_back(pcount);
  depth ++;
  pcount ++;
  for(int ver: pass[now]){
    dfs(ver);
  }
  depth --;
  pcount ++;
  inout[now].second = pcount;
}
signed main() {
  int n;
  cin >> n;
  pass.resize(n);
  tour.resize(n);
  inout.resize(n);
  int p;
  for(int i = 1; i < n; i++){
    cin >> p; 
    p--;
    pass[p].emplace_back(i);
  }
  dfs(0);
  REP(i, n){
    sort(tour[i].begin(), tour[i].end());
  }

  int q;
  cin >> q;
  int u, d;
  REP(i, q){
    cin >> u >> d;
    u--;
    auto in = lower_bound(tour[d].begin(), tour[d].end(), inout[u].first);
    auto out = lower_bound(tour[d].begin(), tour[d].end(), inout[u].second);
    cout << out - in <<endl;
  }
  

  return 0;
}
#include<bits/stdc++.h>
using namespace std;
const int mod=998244353;
int add(int a,int b) {return a+b>=mod?a+b-mod:a+b;}
int dec(int a,int b) {return a<b?a+mod-b:a-b;}
int mul(int a,int b) {return 1ll*a*b%mod;}
void Add(int &a,int b) {(a+=b)>=mod?a-=mod:0;}
void Dec(int &a,int b) {(a-=b)<0?a+=mod:0;}
void Mul(int &a,int b) {a=1ll*a*b%mod;}
int pw2[303];
int c[303][303];
int f[303],g[303];
int a[200503];
int re(){
	int i=0,f=1;
	char c=getchar();
	for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
	for(;isdigit(c);c=getchar()) i=(i<<3)+(i<<1)+c-'0';
	return i*f;
}
int main(){
	c[0][0]=1;
	for(int i=1;i<=300;++i){
		c[i][0]=1;
		for(int j=1;j<=i;++j) c[i][j]=add(c[i-1][j-1],c[i-1][j]);
	}
	pw2[0]=1;
	for(int i=1;i<=300;++i) pw2[i]=add(pw2[i-1],pw2[i-1]);
	int n=re(),m=re();
	for(int i=1;i<=n;++i) a[i]=re();
	int inv2=mod-mod/2;
	for(int i=1;i<=n;++i){
		int buf=1;
		for(int j=0;j<=m;++j){
			Add(f[j],buf);
			Mul(buf,a[i]);
		}
	}
	for(int i=1;i<=m;++i){
		for(int j=0;j<=i;++j){
			Add(g[i],mul(mul(f[j],f[i-j]),c[i][j]));
		}
		Dec(g[i],mul(f[i],pw2[i]));
		Mul(g[i],inv2);
		cout<<g[i]<<endl;
	}
}