#include <bits/stdc++.h>
using namespace std;
using LL = long long int;
#define incID(i, l, r) for(int i = (l)    ; i <  (r); ++i)
#define decID(i, l, r) for(int i = (r) - 1; i >= (l); --i)
#define incII(i, l, r) for(int i = (l)    ; i <= (r); ++i)
#define decII(i, l, r) for(int i = (r)    ; i >= (l); --i)
#define inc(i, n)  incID(i, 0, n)
#define dec(i, n)  decID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define dec1(i, n) decII(i, 1, n)
#define inID(v, l, r) ((l) <= (v) && (v) <  (r))
#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define MT make_tuple
#define FI first
#define SE second
#define FR front()
#define BA back()
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
auto setmin   = [](auto & a, auto b) { return (b <  a ? a = b, true : false); };
auto setmax   = [](auto & a, auto b) { return (b >  a ? a = b, true : false); };
auto setmineq = [](auto & a, auto b) { return (b <= a ? a = b, true : false); };
auto setmaxeq = [](auto & a, auto b) { return (b >= a ? a = b, true : false); };
#define SI(v) static_cast<int>(v.size())
#define RF(e, v) for(auto & e: v)
#define until(e) while(! (e))
#define if_not(e) if(! (e))
#define ef else if
#define UR assert(false)
#define IN(T, ...) T __VA_ARGS__; IN_(__VA_ARGS__);
void IN_() { };
template<typename T, typename ... U> void IN_(T &  a, U &  ... b) { cin >> a; IN_(b ...); };
template<typename T                > void OUT(T && a            ) { cout << a << endl; }
template<typename T, typename ... U> void OUT(T && a, U && ... b) { cout << a << " "; OUT(b ...); }

// ---- ----

template<typename T> vector<T> rerooting(vector<vector<int>> const & g, T const id, function<T(T, T)> f, function<T(T)> u) {
	int n = SI(g);
	vector<T> C(n, id), P(n, id), ans(n);
	function<void(int, int)> dfs1 = [&](int v, int p) {
		RF(e, g[v]) {
			if(e == p) { continue; }
			dfs1(e, v);
			C[v] = f(C[v], u(C[e]));
		}
	};
	function<void(int, int)> dfs2 = [&](int v, int p) {
		int d = SI(g[v]);
		vector<T> L(d + 1, id), R(d + 1, id);
		inc(i, d) {
			int e = g[v][i];
			L[i + 1] = R[i] = u(e == p ? P[v] : C[e]);
		}
		inc(i, d) { L[i + 1] = f(L[i], L[i + 1]); }
		dec(i, d) { R[i]     = f(R[i], R[i + 1]); }
		assert(L.BA == R.FR);
		ans[v] = u(L.BA);
		inc(i, d) {
			int e = g[v][i];
			if(e == p) { continue; }
			P[e] = f(L[i], R[i + 1]);
		}
		RF(e, g[v]) {
			if(e == p) { continue; }
			dfs2(e, v);
		}
	};
	dfs1(0, -1);
	dfs2(0, -1);
	return ans;
}
#define OP2(e) [&](auto A, auto B) { return e; }
#define OP1(e) [&](auto A)         { return e; }

// ----

template<int N = 0> class DynModInt {
private:
	static LL M;
	LL v;
	pair<LL, LL> ext_gcd(LL a, LL b) {
		if(b == 0) { assert(a == 1); return { 1, 0 }; }
		auto p = ext_gcd(b, a % b);
		return { p.SE, p.FI - (a / b) * p.SE };
	}
public:
	DynModInt() { v = 0; }
	DynModInt(LL vv) { assert(M > 0); v = vv; if(abs(v) >= M) { v %= M; } if(v < 0) { v += M; } }
	static void set_M(LL m) { M = m; }
	static LL get_M() { return M; }
	LL get_v() { return v; }
	DynModInt inv() { return ext_gcd(M, v).SE; }
	DynModInt exp(LL b) {
		DynModInt p = 1, a = v; if(b < 0) { a = a.inv(); b = -b; }
		while(b) { if(b & 1) { p *= a; } a *= a; b >>= 1; }
		return p;
	}
	friend bool        operator< (DynModInt   a, DynModInt   b) { return (a.v <  b.v); }
	friend bool        operator> (DynModInt   a, DynModInt   b) { return (a.v >  b.v); }
	friend bool        operator<=(DynModInt   a, DynModInt   b) { return (a.v <= b.v); }
	friend bool        operator>=(DynModInt   a, DynModInt   b) { return (a.v >= b.v); }
	friend bool        operator==(DynModInt   a, DynModInt   b) { return (a.v == b.v); }
	friend bool        operator!=(DynModInt   a, DynModInt   b) { return (a.v != b.v); }
	friend DynModInt   operator+ (DynModInt   a               ) { return DynModInt(+a.v); }
	friend DynModInt   operator- (DynModInt   a               ) { return DynModInt(-a.v); }
	friend DynModInt   operator+ (DynModInt   a, DynModInt   b) { return DynModInt(a.v + b.v); }
	friend DynModInt   operator- (DynModInt   a, DynModInt   b) { return DynModInt(a.v - b.v); }
	friend DynModInt   operator* (DynModInt   a, DynModInt   b) { return DynModInt(a.v * b.v); }
	friend DynModInt   operator/ (DynModInt   a, DynModInt   b) { return a * b.inv(); }
	friend DynModInt   operator^ (DynModInt   a, LL          b) { return a.exp(b); }
	friend DynModInt & operator+=(DynModInt & a, DynModInt   b) { return (a = a + b); }
	friend DynModInt & operator-=(DynModInt & a, DynModInt   b) { return (a = a - b); }
	friend DynModInt & operator*=(DynModInt & a, DynModInt   b) { return (a = a * b); }
	friend DynModInt & operator/=(DynModInt & a, DynModInt   b) { return (a = a / b); }
	friend DynModInt & operator^=(DynModInt & a, LL          b) { return (a = a ^ b); }
	friend istream   & operator>>(istream   & s, DynModInt & b) { s >> b.v; b = DynModInt(b.v); return s; }
	friend ostream   & operator<<(ostream   & s, DynModInt   b) { return (s << b.v); }
};
template<int N> LL DynModInt<N>::M = 0;

// ----

using DMI = DynModInt<>;

int main() {
	IN(int, n, m);
	DMI::set_M(m);
	vector<vector<int>> g(n);
	inc(i, n - 1) {
		IN(int, a, b);
		a--; b--;
		g[a].PB(b);
		g[b].PB(a);
	}
	auto ans = rerooting<DMI>(g, 1, OP2(A * B), OP1(A + 1));
	RF(e, ans) { OUT(e - 1); }
}


Measured memory_bytes: 30212