#include <bits/stdc++.h>

using namespace std;

vector<long> t[111111], s[111111], r[111111];
int m;
int f(int u, int v) {
	int n = t[v].size();
	s[v].resize(n + 1);
	r[v].resize(n);
	s[v][0] = r[v][n - 1] = 1;
	for (int i = n; i--;) {
		s[v][i + 1] = t[v][i] == u ? 1 : f(v, t[v][i]);
		if (i) r[v][i - 1] = (r[v][i] * s[v][i + 1]) % m;
	}
	for (int i = 0; i < n; i++) {
		(s[v][i + 1] *= s[v][i]) %= m;
		(s[v][i] *= r[v][i]) %= m;
	}
	return (s[v][n] + 1) % m;
}
void g(int u, int v, int a) {
	int n = t[v].size();
	(s[v][n] *= a) %= m;
	while (n--) if (t[v][n] != u)
		g(v, t[v][n], (s[v][n] * a + 1) % m);
}
int main() {
	int n;
	cin >> n >> m;
	for (int i = 1; i < n; i++) {
		int x, y;
		scanf("%d%d", &x, &y);
		t[x].push_back(y);
		t[y].push_back(x);
	}
	if (n < 2) {
		cout << 1 << endl;
		return 0;
	}
	f(0, 1), g(0, 1, 1);
	for (int i = 1; i <= n; i++)
		cout << s[i][t[i].size()] << '\n';
	cout << flush;
	return 0;
}
