using System.Collections.Generic;
using System.IO;
using System.Linq;
using static System.Console;
class Z { static void Main() => new K(); }
class K
{
	int[] G => ReadLine().Split().Select(int.Parse).ToArray();
	public K()
	{
		SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false });
		Solve();
		Out.Flush();
	}
	int V, Mod;
	List<int>[] es;
	int[] par;
	long[] dp0, dp1;
	void Solve()
	{
		var I = G;
		V = I[0];
		Mod = I[1];
		es = new List<int>[V];
		par = new int[V];
		dp0 = new long[V];
		dp1 = new long[V];
		for (var u = 0; u < V; u++) es[u] = new List<int>();
		for (var e = 0; e < V - 1; e++)
		{
			I = G;
			es[I[0] - 1].Add(I[1] - 1);
			es[I[1] - 1].Add(I[0] - 1);
		}
		DFS1Exe(0, -1);
		DFS2Exe(0);
		DFS3Exe(0);
		for (var u = 0; u < V; u++) WriteLine(dp0[u] * (dp1[u] + 1) % Mod);
	}
	void DFS1Exe(int u, int p)
	{
		var st = new Stack<object>();
		st.Push(new A0 { u = u, p = p });
		while (st.Count > 0)
		{
			var f = st.Pop();
			if (f is A0)
			{
				var g = (A0)f;
				par[g.u] = g.p;
				es[g.u].Remove(g.p);
				st.Push(new A1 { u = g.u, p = g.p, i = 0 });
			}
			else
			{
				var g = (A1)f;
				if (g.i < es[g.u].Count)
				{
					st.Push(new A1 { u = g.u, p = g.p, i = g.i + 1 });
					st.Push(new A0 { u = es[g.u][g.i], p = g.u });
				}
			}
		}
	}
	void DFS2Exe(int u)
	{
		var st = new Stack<object>();
		st.Push(new B0 { u = u });
		while (st.Count > 0)
		{
			var f = st.Pop();
			if (f is B0)
			{
				var g = (B0)f;
				dp0[g.u] = 1L;
				st.Push(new B1 { u = g.u, i = 0 });
			}
			else if (f is B1)
			{
				var g = (B1)f;
				if (g.i < es[g.u].Count)
				{
					st.Push(new B2 { u = g.u, i = g.i });
					st.Push(new B0 { u = es[g.u][g.i] });
				}
			}
			else
			{
				var g = (B2)f;
				dp0[g.u] = dp0[g.u] * (dp0[es[g.u][g.i]] + 1) % Mod;
				st.Push(new B1 { u = g.u, i = g.i + 1 });
			}
		}
	}
	void DFS3Exe(int u)
	{
		var st = new Stack<object>();
		st.Push(new C0 { u = u });
		while (st.Count > 0)
		{
			var f = st.Pop();
			if (f is C0)
			{
				var g = (C0)f;
				var W = es[g.u].Count;
				var x = new long[W + 1];
				var y = new long[W + 1];
				x[0] = y[W] = 1;
				for (var i = 0; i < W; i++) x[i + 1] = x[i] * (dp0[es[g.u][i]] + 1) % Mod;
				for (var i = W - 1; i >= 0; i--) y[i] = y[i + 1] * (dp0[es[g.u][i]] + 1) % Mod;
				for (var i = 0; i < W; i++) dp1[es[g.u][i]] = (dp1[g.u] + 1) * x[i] % Mod * y[i + 1] % Mod;
				x = y = null;
				st.Push(new C1 { u = g.u, i = 0 });
			}
			else
			{
				var g = (C1)f;
				if (g.i < es[g.u].Count)
				{
					st.Push(new C1 { u = g.u, i = g.i + 1 });
					st.Push(new C0 { u = es[g.u][g.i] });
				}
			}
		}
	}
	void DFS2(int u)
	{
	b0:
		dp0[u] = 1L;
		var i = 0;
	b1:
		if (i < es[u].Count)
		{
			DFS2(es[u][i]);
		b2:
			dp0[u] = dp0[u] * (dp0[es[u][i]] + 1) % Mod;
			i++;
			goto b1;
		}
	}
	void DFS3(int u)
	{
	c0:
		{
			var W = es[u].Count;
			var x = new long[W + 1];
			var y = new long[W + 1];
			x[0] = y[W] = 1;
			for (var i = 0; i < W; i++) x[i + 1] = x[i] * (dp0[es[u][i]] + 1) % Mod;
			for (var i = W - 1; i >= 0; i--) y[i] = y[i + 1] * (dp0[es[u][i]] + 1) % Mod;
			for (var i = 0; i < W; i++) dp1[es[u][i]] = (dp1[u] + 1) * x[i] % Mod * y[i + 1] % Mod;
			x = y = null;
		}
		var j = 0;
	c1:
		if (j < es[u].Count)
		{
			DFS3(es[u][j]);
		c2:
			j++;
			goto c1;
		}
	}
}
struct A0 { public int u, p; }
struct A1 { public int u, p, i; }
struct B0 { public int u; }
struct B1 { public int u, i; }
struct B2 { public int u, i; }
struct C0 { public int u; }
struct C1 { public int u, i; }
struct C2 { public int u, i; }

Measured memory_bytes: 38936