// #include <boost/multiprecision/cpp_int.hpp> 
// using namespace boost::multiprecision; 
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define f first
#define s second
#define pb push_back
#define REP(i,a,b) for(long long int i=a ; i<b ; i++)
#define mp make_pair
#define INF 1000000000000000000
// #define mod 1000000007
#define pi pair<ll,ll>
#define pd pair<ld,ld>
#define vi vector<ll>
#define vd vector<ld>
#define vvi vector<vi>
#define vvd vector<vd>
#define vpi vector<pi>
#define vpd vector<pd>
#define us unordered_set<ll>
#define um unordered_map<ll,ll>
#define sortA(a) sort(a.begin(),a.end())
#define sortD(a) sort(a.begin(),a.end(),greater<ll>())
#define all(a) a.begin(),a.end()
#define N 100005


#include <ext/pb_ds/assoc_container.hpp> 
using namespace __gnu_pbds; 
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> new_data_set; 
#define in insert
#define fbo find_by_order
#define ook order_of_key


void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
void print1D(vi a,ll n)
{
	for(ll i=0;i<n;++i)
	{
		cout<<a[i]<<endl;
	}
}
void print2D(vector<vi> a)
{
	for(ll i=0;i<a.size();++i)
	{
		for(ll j=0;j<a[i].size();++j)
		{
			cout<<a[i][j]<<" ";
		}
		cout<<endl;
	}
}
vvi G(N);
ll n,mod;
vi B(N);
vi ans(N);
void dfs(ll i,ll par)
{
	B[i]=1;
	for(ll j=0;j<G[i].size();++j)
	{
		if(G[i][j]!=par)
		{
			dfs(G[i][j],i);
			B[i]=(B[i]*(B[G[i][j]]+1))%mod;
		}
	}
}
void rerooting(ll i,ll par)
{
	vi pref,suff;
	ll pprod=1,sprod=1;
	ll sz=G[i].size();
	for(ll j=0;j<sz;++j)
	{
		pprod=(pprod*(B[G[i][j]]+1))%mod;
		sprod=(sprod*(B[G[i][sz-1-j]]+1))%mod;
		pref.pb(pprod);
		suff.pb(sprod);
	}
	reverse(suff.begin(),suff.end());
	for(ll j=0;j<sz;++j)
	{
		if(G[i][j]!=par)
		{
			ll t1=B[i],t2=B[G[i][j]];
			if(j==0 && j==sz-1)
			{
				B[i]=1;
			}
			else if(j==0)
			{
				B[i]=suff[j+1];
			}
			else if(j==sz-1)
			{
				B[i]=pref[j-1];
			}
			else
			{
				B[i]=(pref[j-1]*suff[j+1])%mod;
			}
			B[G[i][j]]=(B[G[i][j]]*(B[i]+1))%mod;
			ans[G[i][j]]=B[G[i][j]];
			rerooting(G[i][j],i);
			B[i]=t1;
			B[G[i][j]]=t2;
		}
	}
}
void solve()
{
	cin>>n>>mod;
	for(ll i=1;i<=n-1;++i)
	{
		ll u,v;
		cin>>u>>v;
		u--;
		v--;
		G[u].pb(v);
		G[v].pb(u);
	}
	dfs(0,0);
	ans[0]=B[0];
	rerooting(0,0);
	print1D(ans,n);
}
int main()
{
	ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
	ll t=1;
	// cin>>t;
	while(t--)
	{
		solve();
	}
}


Measured memory_bytes: 26752