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

//#define getchar getchar_unlocked
//#define putchar putchar_unlocked
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define mp make_pair
#define ff first
#define ss second
#define err stderr
#define out stdout

typedef long long int ll;
typedef long double ld;
typedef short int sh;

inline void readI(int *i)
{
	register int t=0;
	register char z=getchar();
	int znak=1;
	if (z=='-')
	{
		znak=-1;
	}
	while ((z<'0') || ('9'<z))
	{
		z=getchar();
		if (z=='-')
		{
			znak=-1;
		}
	}
	while (('0'<=z) && (z<='9'))
	{
		t=(t<<3)+(t<<1)+z-'0';
		z=getchar();
	}
	*i=(t*znak);
}
inline void readUI(int *i)
{
	register int t=0;
	register char z=getchar();
	while ((z<'0') || ('9'<z))
	{
		z=getchar();
	}
	while (('0'<=z) && (z<='9'))
	{
		t=(t<<3)+(t<<1)+z-'0';
		z=getchar();
	}
	*i=t;
}
inline void readL(ll *l)
{
	register ll t=0;
	register char z=getchar();
	int znak=1;
	if (z=='-')
	{
		znak*=-1;
	}
	while ((z<'0') || ('9'<z))
	{
		z=getchar();
		if (z=='-')
		{
			znak=-1;
		}
	}
	while (('0'<=z) && (z<='9'))
	{
		t=(t<<3)+(t<<1)+z-'0';
		z=getchar();
	}
	*l=(t*znak);
}
inline void readUL(ll *l)
{
	register ll t=0;
	register char z=getchar();
	while ((z<'0') || ('9'<z))
	{
		z=getchar();
	}
	while (('0'<=z) && (z<='9'))
	{
		t=(t<<3)+(t<<1)+z-'0';
		z=getchar();
	}
	*l=t;
}
inline void writeL(ll l)
{
	if (l==0)
	{
		putchar(48);
	}
	else
	{
		if (l<0)
		{
		   l*=-1;
		   putchar(45);
		}
		int _tab[21];
		int wsk=0;
		while (l>0)
		{
			++wsk;
			_tab[wsk]=(l%10)+48;
			l/=10;
		}
		for (int j=wsk; j>=1; --j)
		{
			putchar(_tab[j]);
		}
	}
}
inline void writeS(string s)
{
	int l=s.length();
	for (int i=0; i<l; ++i)
	{
		putchar(s[i]);
	}
}
inline void space()
{
	putchar(32);
}
inline void endl()
{
	putchar(10);
}

#define debug if(0)
#define debug2 if(1)
#define debug3 if(1)
#define debug4 if(1)
#define MAXN (int)(1e5)+5
#define ZAK (1<<18)+5

int n, a, b, R;
ll dp[MAXN], DP[MAXN];
vector <int> v[MAXN];
ll P;
bitset <MAXN> odw;
ll tree[ZAK];

void test_dp()
{
	for (int i=1; i<=n; ++i)
	{
		fprintf (err, "i=%d dp[i]=%lld DP[i]=%lld\n", i, dp[i], DP[i]);
	}
}

void dfs(int x)
{
	odw[x]=true;
	int l=v[x].size();
	dp[x]=1;
	for (int i=0; i<l; ++i)
	{
		if (!odw[v[x][i]])
		{
			dfs(v[x][i]);
			dp[x]*=(dp[v[x][i]]+1);
			dp[x]%=P;
		}
	}
}
void insert(int x, int pp, int pk, int zp, int zk, ll war)
{
	if (zk<pp || pk<zp)
	{
		return;
	}
	if (zp<=pp && pk<=zk)
	{
		tree[x]=(tree[x]*war)%P;
		return;
	}
	int sr=((pp+pk)>>1);
	insert(x<<1, pp, sr, zp, zk, war);
	insert((x<<1)+1, sr+1, pk, zp, zk, war);
}
ll query(int x)
{
	x+=R-1;
	ll ilo=1;
	while (x)
	{
		ilo*=tree[x];
		ilo%=P;
		x>>=1;
	}
	return ilo;
}
void dfs2(int x)
{
	odw[x]=true;
	int l=v[x].size();
	R=1;
	int roz=l+(x!=1);
	while (R<roz)
	{
		R<<=1;
	}
	for (int i=1; i<(R<<1); ++i)
	{
		tree[i]=1;
	}
	if (x!=1)
	{
		insert(1, 1, R, 1, roz-1, DP[x]+1);
	}
	for (int i=0; i<l; ++i)
	{
		if (!odw[v[x][i]])
		{
			if (i>0)
			{
				insert(1, 1, R, 1, i, dp[v[x][i]]+1);
			}
			if (i<roz-1)
			{
				insert(1, 1, R, i+2, roz, dp[v[x][i]]+1);
			}
		}
	}
	for (int i=0; i<l; ++i)
	{
		if (!odw[v[x][i]])
		{
			DP[v[x][i]]=query(i+1);
		}
	}
	for (int i=0; i<l; ++i)
	{
		if (!odw[v[x][i]])
		{
			dfs2(v[x][i]);
		}
	}
}
int main()
{
	readUI(&n);
	readUL(&P);
	for (int i=1; i<n; ++i)
	{
		readUI(&a);
		readUI(&b);
		v[a].eb(b);
		v[b].eb(a);
	}
	dfs(1);
	odw.reset();
	dfs2(1);
	debug
	{
		test_dp();
	}
	for (int i=1; i<=n; ++i)
	{
		fprintf (out, "%lld\n", (dp[i]*(DP[i]+1))%P);
	}
	return 0;
}
/*
*/




Measured memory_bytes: 11648