#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eb emplace_back
#define mt make_tuple
#define emp emplace

using namespace std;
using ll = long long;
using ii = pair<int, int>;

const int N = 100010;
int MOD;
vector<int> g[N];

int mul(ll a, ll b)
{
	return (a*b)%MOD;
}


int pd[N];
int pd2[N];

int solve(int u, int last)
{
	pd[u] = 1;
	for (int v : g[u])
	{
		if (v == last) continue;
		pd[u] = mul(pd[u], 1 + solve(v, u));
	}
	return pd[u];
}

void solve2(int u, int last, int up)
{
	pd2[u] = mul(pd[u], up + 1);

	vector<int> pref;
	pref.pb(1);
	for (int i = 0; i < g[u].size(); i++)
	{
		int v = g[u][i];
		int val = v == last ? 1 : pd[v] + 1;
		pref.pb(mul(pref.back(), val));
	}
	int suf = 1;
	for (int i = (int)g[u].size()-1; i >= 0; i--)
	{
		int v = g[u][i];
		if (v == last) continue;
		
		solve2(v, u, mul(mul(up+1, pref[i]), suf));
		suf = mul(suf,  pd[v] + 1);
	}
}

int main()
{
	int n;
	scanf("%d %d", &n, &MOD);
	for (int i = 0; i < n-1; i++)
	{
		int u, v;
		scanf("%d %d", &u, &v);
		g[u].pb(v);
		g[v].pb(u);
	}
	solve(1, 0);
	solve2(1, 0, 0);
	for (int i = 1; i <= n; i++)
		printf("%d\n", pd2[i]);
	return 0;
}