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

#define   fi(a,b) for(int i=a;i<b;i++)
#define   fj(a,b) for(int j=a;j<b;j++)
#define   ff first
#define   ss second
#define   ll long long
#define   ld long double
#define   ull unsigned long long
#define   bp(x) __builtin_popcount(x)
#define   pr(x) for(auto it: x) cout<<it<<" "; cout<<endl;
#define   getMax(x) max_element(x.begin(),x.end())
#define   getMin(x) min_element(x.begin(),x.end())
#define   endl "\n"
typedef   vector<int> vi;
typedef   vector< pair<int, int> > vii;
typedef   vector<long long> vl;
typedef   pair<int, int> pii;
typedef   pair<ll, ll> pll;
typedef   vector< pair<ll, ll> > vll;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// auto dist = uniform_int_distribution<int>(l, r);
// int dx[] = {1, 0, -1, 0};
// int dy[] = {0, 1, 0, -1};
// int dx[] = { -1, 0, 1, 1, 1, 0, -1, -1};
// int dy[] = { -1, -1, -1, 0, 1, 1, 1, 0};

vector<vi>adj;
vl depth, in, out, ans;
vector<vi> cache;
int tym = 0;


void dfs(int u = 0, int pre = -1, int dep = 0) {
	depth[u] = dep;
	in[u] = tym++;
	cache[dep].push_back(in[u]);
	for (auto v : adj[u]) {
		if (v == pre) continue;
		dfs(v, u, dep + 1);
	}
	out[u] = tym++;
}

bool isAnc(int u, int v) {
	return (in[u] <= in[v] and out[u] >= out[v]);
}

void omae_wa_mou_shindeiru(int tc) {
	int n;
	cin >> n;
	depth.assign(n, 0);
	adj.assign(n, vi({}));
	in.assign(n, 0);
	out.assign(n, 0);
	cache.assign(n + 5, vi({}));
	fi(1, n) {
		int v;
		cin >> v;
		v--;
		adj[i].push_back(v);
		adj[v].push_back(i);
	}
	dfs();
	int q;
	cin >> q;
	ans.assign(q, 0);
	fi(0, q) {
		ll u, d;
		cin >> u >> d;
		u--, d;
		cout << lower_bound(cache[d].begin(), cache[d].end(), out[u]) - lower_bound(cache[d].begin(), cache[d].end(), in[u]) << endl;
	}
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int tc = 1;
	// cin >> tc;
	fi(1, tc + 1) {
		omae_wa_mou_shindeiru(i);
	}
}

#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,s,n) for (int i = (s); i < (n); ++i)
#define rrep(i,n,g) for (int i = (n)-1; i >= (g); --i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define len(x) (int)(x).size()
#define dup(x,y) (((x)+(y)-1)/(y))
#define pb push_back
#define eb emplace_back
#define Field(T) vector<vector<T>>
#define pq(T) priority_queue<T,vector<T>,greater<T>>
using namespace std;
using ll = long long;
using P = pair<int,int>;

struct ArbitraryModInt {
  int x;
  ArbitraryModInt() : x(0) {}
  ArbitraryModInt(int64_t y) : x(y >= 0 ? y % get_mod() : (get_mod() - (-y) % get_mod()) % get_mod()) {}

  static int &get_mod() {
    static int mod = 0;
    return mod;
  }
  static void set_mod(int md) {
    get_mod() = md;
  }

  ArbitraryModInt &operator+=(const ArbitraryModInt &p) {
    if((x += p.x) >= get_mod()) x -= get_mod();
    return *this;
  }
  ArbitraryModInt &operator-=(const ArbitraryModInt &p) {
    if((x += get_mod() - p.x) >= get_mod()) x -= get_mod();
    return *this;
  }
  ArbitraryModInt &operator*=(const ArbitraryModInt &p) {
    unsigned long long a = (unsigned long long) x * p.x;
    unsigned xh = (unsigned) (a >> 32), xl = (unsigned) a, d, m;
    asm("divl %4; \n\t" : "=a" (d), "=d" (m) : "d" (xh), "a" (xl), "r" (get_mod()));
    x = m;
    return *this;
  }
  ArbitraryModInt &operator/=(const ArbitraryModInt &p) {
    *this *= p.inv();
    return *this;
  }
  ArbitraryModInt operator-() const { return ArbitraryModInt(-x); }
  ArbitraryModInt operator+(const ArbitraryModInt &p) const { return ArbitraryModInt(*this) += p; }
  ArbitraryModInt operator-(const ArbitraryModInt &p) const { return ArbitraryModInt(*this) -= p; }
  ArbitraryModInt operator*(const ArbitraryModInt &p) const { return ArbitraryModInt(*this) *= p; }
  ArbitraryModInt operator/(const ArbitraryModInt &p) const { return ArbitraryModInt(*this) /= p; }
  bool operator==(const ArbitraryModInt &p) const { return x == p.x; }
  bool operator!=(const ArbitraryModInt &p) const { return x != p.x; }

  ArbitraryModInt inv() const {
    int a = x, b = get_mod(), u = 1, v = 0, t;
    while(b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ArbitraryModInt(u);
  }
  ArbitraryModInt pow(int64_t n) const {
    ArbitraryModInt ret(1), mul(x);
    while(n > 0) {
      if(n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }
  friend ostream &operator<<(ostream &os, const ArbitraryModInt &p) {
    return os << p.x;
  }
  friend istream &operator>>(istream &is, ArbitraryModInt &a) {
    int64_t t;
    is >> t;
    a = ArbitraryModInt(t);
    return (is);
  }
};

using mint = ArbitraryModInt;

void mul(vector<mint> &dp, int d) {
  int MAX = len(dp)-1;
  rrep(i,MAX+1,d) {
    dp[i] -= dp[i - d];
  }
}

void div(vector<mint> &dp, int d) {
  int MAX = len(dp);
  rep(i,d,MAX+1) {
    dp[i] += dp[i - d];
  }
}

int main() {
  int n, k, m;
  cin >> n >> k >> m;
  mint::set_mod(m);
  vector<mint> dp(n*n*k+1, 0);
  vector<vector<mint>> vals(n+1, vector<mint>(n*n*k+1));
  dp[0] = mint(1);
  vals[0][0] = dp[0];
  rep(i,1,n) {
    div(dp, i);
    mul(dp, i*(k+1));
    rep(j,0,n*n*k) {
      vals[i][j] = dp[j];
    }
  }
  rep(i,0,n) {
    mint ans = 0;
    rep(j,0,n*n*k+1) {
      ans += vals[i][j] * vals[n-i-1][j];
    }
    cout << ans*(k+1)-1 << endl;
  }
  return 0;
}
