#include <iostream>
#include <bits/stdc++.h>

using namespace std;
//fold
#define ll long long
#define pb push_back
#define pf push_front
#define lb lower_bound
#define ub upper_bound
#define Loop(i, a, b) for(int i = a ; i < b ; ++i)
#define loop(i, n) Loop(i, 0, n)
#define ff first
#define ss second
#define mkp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define vvi vector<vi>
#define vii vector<pii>
#define vvii vector<vii>
#define vl vector<ll>
#define pll pair<ll, ll>
#define vll vector<pll>
#define vvll vector<vll>
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define nmbr(c) (int)(c-'0')
#define ltr(c) (int)(c-'a')
#define INF 1e18+5
#define inf 1e9+5
//#define M 1000000007 //check
#define _69e27 ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define trace(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

template <typename T> T max(T a, T b, T c){ return max(a, max(b, c));}
template <typename T> T min(T a, T b, T c){ return min(a, min(b, c));}
//endfold
 
// parent/down[cur_node]
/* 
 * maintain array that stores down of all children and the parent
 * base: if parent is 1 --> array[parent][cur_node] = down[children]/down[cur_node]
 * for other cases array[parent][cur_node] = down[children]/down[cur_node] * array[grand_parent][parent]
 */

const int nax = 1e5+5;
ll down[nax], up[nax];
int n, M;
pii parent[nax];
vvi e(nax);
vector<vl> children(nax);

ll mul(ll a, ll b){return a*b % M;}
ll add(ll a, ll b){return (a+b) % M;}

void black(int cur_node, int last_node)
{
	ll &ans = down[cur_node];
	ans = 1;
	int s = sz(e[cur_node]);
	loop(i, s)
	{
		int it = e[cur_node][i];
		if(it == last_node) continue;
		parent[it] = mkp(cur_node, i);
		black(it, cur_node);
		ans = mul(ans, down[it] +1);
	}
	children[cur_node].resize(s+1);
}

void adjust(int cur_node, int last_node)
{
	// calculate children
	int s = sz(e[cur_node]);
	vl L(s+1), R(s+1);
	loop(i, s)
	{
		L[i] = add(down[e[cur_node][i]],1);
		if(e[cur_node][i] == last_node) L[i] = 1;
		if(i) L[i] = mul(L[i], L[i-1]);
		R[s-i-1] = add(down[e[cur_node][s-i-1]],1);
		if(e[cur_node][s-i-1] == last_node) R[s-i-1] = 1;
		if(i) R[s-i-1] = mul(R[s-i-1], R[s-i]);
	}

	// parent/down[cur_node] ..
	ll adjusted_parent = 1;
	if(cur_node != 1)
	{
		int index = parent[cur_node].ss;
		adjusted_parent = children[last_node][index];
	}

	loop(i, s)
	{
		int it = e[cur_node][i];
		if(it == last_node) continue;

		ll l = i ? L[i-1]:1, r = (i != s-1) ? R[i+1]:1;
		
		int children_index = parent[it].ss; 
		children[cur_node][children_index] = add(mul(mul(l, r), adjusted_parent), 1);
		up[it] = mul(down[it], children[cur_node][children_index]);
		adjust(it, cur_node);
	}
}
int main() {
	_69e27;
	cin >> n >> M;
	loop(i, n-1)
	{
		int in1, in2;
		cin >> in1 >> in2;
		e[in1].pb(in2);
		e[in2].pb(in1);;
	}
	
	black(1, 0);
	up[1] = down[1];
	adjust(1, 0);
	loop(i, n) cout << up[i+1]%M << endl;
	return 0;
}


Measured memory_bytes: 26112