#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int m, pa[100010];
vector<int> e[100010];
long long dp[100010], dp2[100010];
void dfs(int v, int p = -1) {
	dp[v] = 1;
	for (int i = 0; i < e[v].size(); i++) {
		int w = e[v][i];
		if (w == p) continue;
		pa[w] = v;
		dfs(w, v);
		dp[v] = dp[v] * (1 + dp[w]) % m;
	}
	dp2[v] = p == -1 ? 0 : 1;
}
void dfs2(int v, int p = -1) {
	vector<int> t = e[v];
	for (int k = 0; k < 2; k++) {
		long long x = 1;
		for (int i = 0; i < t.size(); i++) {
			int w = t[i];
			if (w == p) {
				x = x * (1 + dp2[v]) % m;
			} else {
				dp2[w] = dp2[w] * x % m;
				x = x * (1 + dp[w]) % m;
			}
		}
		reverse(t.begin(), t.end());
	}
	for (int i = 0; i < t.size(); i++) {
		if (t[i] != p) dfs2(t[i], v);
	}
}
int main() {
	int n;
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n - 1; i++) {
		int u, v;
		scanf("%d%d", &u, &v);
		--u; --v;
		e[u].push_back(v);
		e[v].push_back(u);
	}
	dfs(0);
	dfs2(0);
	for (int i = 0; i < n; i++) {
		long long ans = dp[i] * (1 + dp2[i]) % m;
		printf("%lld\n", ans);
	}
	return 0;
}
