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


// type definitions
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector< vi > vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef complex<int> ci;
typedef complex<double> cd;
typedef complex<long double> cld;

// macros
#define rep(i, a, b) for(int i=a; i<b; i++)
#define TIME clock() * 1.0 / CLOCKS_PER_SEC
#define all(c) c.begin(), c.end()
#define tr(c, it) for(auto it=(c).begin(); it!=c.end(); it++)
#define pb push_back
#define mp make_pair
#define bitcount __builtin_popcount
#define ispresent(c, x) ((c).find(x) != (c).end())
#define watch(x) cout<< (#x) << " is " << (x) <<"\n";
#define sz(x) ((int)((x).size()))
#define UNIQUE(c) (c).resize(unique(all(c)) - (c).begin())
#define pii2ll(p) ((ll)(p).first<<32 | (p).second)

// template functions
template <typename T>
inline void printV(vector<T>& a) { rep(i, 0, sz(a)) { cout<<a[i]<<" "; } cout<<"\n"; }
template <typename T>
inline void printV(vector<T>& a, int n) { rep(i, 0, n) { cout<<a[i]<<" "; } cout<<"\n"; }
template <typename T>
inline void printVV(vector<vector<T>>& a)
{
	rep(i, 0, sz(a)) {
		printV(a[i]);
	}
	cout<<"\n";
}

template <typename T>
inline void clearVV(vector<vector<T>>& a)
{
	for(auto& x:a) {
		x.clear();
	}
	a.clear();
}
template <typename T>
inline void assignVV(vector<vector<T>> &a, T val)
{
	for(int i=0; i<sz(a); i++) {
		for(int j=0; j<sz(a[i]); j++) {
			a[i][j]=val;
		}
	}
}


// constants
const long double eps = 1e-9;
const int inf = 5e8;
const int modn = 1e9+7;
const int MAX = 1e5;

int m;
int phi;
inline int add(int a, int b)
{
	return ((ll)a+b)%m;
}

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

void sieve()
{
	int tmp=m;
	phi=1;
	for(int i=2; (ll)i*i<=tmp; i++) {
		if(tmp%i==0) {
			phi*=i-1;
			while(tmp%i==0) {
				tmp/=i;
			}
		}
	}
	if(tmp>1) phi*=tmp-1;
}

int po(int a, int b)
{
	int res=1;
	while(b>0) {
		if(b&1) {
			res=mul(res, a);
		}
		a=mul(a, a);
		b>>=1;
	}
	return res;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    // Your code here
	int n;
	cin>>n>>m;
	int tmpm=m;
	sieve();
	vector<vector<int>> adj(n);
	for(int i=0; i<n-1; i++) {
		int x, y;
		cin>>x>>y;
		x--, y--;
		adj[x].push_back(y);
		adj[y].push_back(x);
	}
	
	vector<int> parent(n);
	function<void(int, int)> dfsp = [&](int u, int pr)
	{
		parent[u]=pr;
		for(int v:adj[u]) {
			if(v!=pr) {
				dfsp(v, u);
			}
		}
	};
	dfsp(0, -1);
	
	vector<int> val(n);
	vector<bool> marked(n);
	function<void(int, int)> solve = [&](int u, int pr)
	{
		val[u]=1;
		for(int v:adj[u]) {
			if(v!=pr) {
				solve(v, u);
				val[u]=mul(val[u], 1+val[v]);
			}
		}
	};
	solve(0, -1);
	//~ printV(val);
	
	vector<int> dp(n);
	function<void(int, int)> dfsup = [&](int u, int pr)
	{
		int prev=1+dp[u];
		vector<int> child;
		for(int v:adj[u]) {
			if(v!=pr) {
				child.push_back(v);
			}
		}
		vector<int> pref(sz(child));
		vector<int> suf(sz(child)+1);
		suf[sz(child)]=1;
		for(int i=0; i<sz(child); i++) {
			pref[i]=(i==0?1+val[child[i]]:mul(pref[i-1], 1+val[child[i]]));
		}
		for(int i=sz(child)-1; i>=0; i--) {
			suf[i]=(i==sz(child)-1?1+val[child[i]]:mul(suf[i+1], 1+val[child[i]]));
		}
		for(int i=0; i<sz(child); i++) {
			dp[child[i]]=(i==0?suf[i+1]:mul(pref[i-1], suf[i+1]));
			dp[child[i]]=mul(dp[child[i]], prev);
		}
		val[u]=mul(val[u], prev);
		for(int v:adj[u]) {
			if(v!=pr) {
				dfsup(v, u);
			}
		}
	};
	dfsup(0, -1);
	
	for(int i=0; i<n; i++) {
		cout<<val[i]%tmpm<<"\n";
	}
    return 0;
}



Measured memory_bytes: 24832