using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using static System.Console;
using static System.Math;
using static MyIO;
using static MyUtil;

public class V { public static void Main() => (new Solver()).Solve(); }

public class Solver
{
	public void Solve()
	{
		long N = GetLong();
		long M = GetLong();

		MInt.MOD = M;

		Func<MInt, long, MInt> f = (x, _) => x + 1;
		Func<MInt, long, MInt> g = (x, _) => x;
		Func<MInt, MInt, MInt> merge = (x, y) => x * y;
		var re = new ReRooting<MInt>(N, f, g, merge, new MInt(1));
		for(long i = 0; i < N - 1; i++)
		{
			long x = GetLong() - 1;
			long y = GetLong() - 1;
			re.AddEdge(x, y);
		}
		re.Calc();
		WriteLine(string.Join("\n", Range(0, N).Select(x => re.Solve(x).Val)));
	}
}

public struct MInt
{
	public static long MOD = 1000000007;
	private static MInt[] f = null;
	private static MInt[] g = null;
	public long Val { get; private set; }

	public MInt(long v)
	{
		Val = (v + MOD) % MOD;
	}

	public static void InitTable(int N) => InitTable((long)N);
	public static void InitTable(long N)
	{
		f = new MInt[N + 1];
		f[0] = new MInt(1);
		for(long i = 1; i <= N; i++)
			f[i] = f[i - 1] * i;

		g = new MInt[N + 1];
		g[N] = MPow(f[N], MOD - 2);
		for(long i = N; i > 0; i--)
			g[i - 1] = g[i] * i;
	}

	public static MInt MComb(long n, long k)
	{
		if(n < 0 || n < k)
			return new MInt(0);
			
		return f[n] * g[k] * g[n - k];
	}

	public static MInt operator+(MInt x, long y) => x + new MInt(y);	
	public static MInt operator+(long x, MInt y) => new MInt(x) + y;
	public static MInt operator+(MInt x, MInt y)
	{
		long tmp = x.Val + y.Val;
		if(tmp >= MOD)
			tmp -= MOD;
		return new MInt(tmp);
	}

	public static MInt operator-(MInt x, long y) => x - new MInt(y);
	public static MInt operator-(long x, MInt y) => new MInt(x) - y;
	public static MInt operator-(MInt x, MInt y)
	{
		long tmp = x.Val;
		if(tmp < y.Val)
			tmp += MOD;
		tmp -= y.Val;
		return new MInt(tmp);
	}

	public static MInt operator*(MInt x, MInt y) => new MInt((x.Val * y.Val) % MOD);
	public static MInt operator*(MInt x, long y) => x * new MInt(y);
	public static MInt operator*(long x, MInt y) => new MInt(x) * y;

	public static MInt operator/(MInt x, MInt y) => x * MPow(y, MOD - 2);
	public static MInt operator/(MInt x, long y) => x * MPow(y, MOD - 2);
	public static MInt operator/(long x, MInt y) => x * MPow(y, MOD - 2);

	public static MInt MPow(MInt x, MInt y) => MPow(x.Val, y.Val);
	public static MInt MPow(MInt x, long y) => MPow(x.Val, y);
	public static MInt MPow(long x, MInt y) => MPow(x, y.Val);
	public static MInt MPow(long x, long y)
	{
		long exp = y;
		long xx = x;
		long res = 1;
		while(exp > 0)
		{
			if((exp & 1) == 1)
				res = (res * xx) % MOD;

			xx = (xx * xx) % MOD;
			exp >>= 1;
		}
		return new MInt(res);
	}
}

// https://null-mn.hatenablog.com/entry/2020/04/14/124151
public class ReRooting<T>
{
	private long N;
	private Func<T, long, T> f;
	private Func<T, long, T> g;
	private Func<T, T, T> merge;
	private T ie;

	private List<long>[] adj;
	private T[][] dp;

	public ReRooting(long N, Func<T, long, T> f, Func<T, long, T> g, Func<T, T, T> merge, T ie)
	{
		this.N = N;
		this.f = f;
		this.g = g;
		this.merge = merge;
		this.ie = ie;

		adj = Enumerable.Range(0, (int)N).Select(_ => new List<long>()).ToArray();
		dp = new T[N][];
	}

	public void AddEdge(long a, long b)
	{
		adj[a].Add(b);
		adj[b].Add(a);
	}

	private T dfs1(long p, long v)
	{
		var res = ie;
		for(long i = 0; i < adj[v].Count; i++)
		{
			long u = adj[v][(int)i];
			if(u == p) continue;
			dp[v][i] = dfs1(v, u);
			res = merge(res, f(dp[v][i], u));
		}
		return g(res, v);
	}

	private void dfs2(long p, long v, T add)
	{
		long count = adj[v].Count;
		for(long i = 0; i < count; i++)
		{
			long u = adj[v][(int)i];
			if(u == p)
			{
				dp[v][i] = add;
				break;
			}
		}

		var pl = new T[count + 1];
		var pr = new T[count + 1];
		pl[0] = ie;
		for(long i = 0; i < count; i++)
			pl[i + 1] = merge(pl[i], f(dp[v][i], adj[v][(int)i]));

		pr[count] = ie;
		for(long i = count; i > 0; i--)
			pr[i - 1] = merge(pr[i], f(dp[v][i - 1], adj[v][(int)i - 1]));

		for(long i = 0; i < count; i++)
		{
			long u = adj[v][(int)i];
			if(u == p) continue;
			var val = merge(pl[i], pr[i + 1]);
			dfs2(v, u, g(val, v));
		}
	}

	public void Calc(long root = 0)
	{
		for(long i = 0; i < N; i++)
			dp[i] = new T[adj[i].Count];

		dfs1(-1, root);
		dfs2(-1, root, ie);
	}

	public T Solve(long v)
	{
		var ans = ie;
		for(long i = 0; i < adj[v].Count; i++)
			ans = merge(ans, f(dp[v][i], adj[v][(int)i]));

		return g(ans, v);
	}
}

public static class MyIO
{
	private static string[] args = new string[0];
	private static int cnt = 0;
	private static IEnumerable<string> getArgs(long N) => Range(0, N).Select(_ => getArg());
	private static string getArg()
	{
		if(cnt == args.Length) { args = Console.ReadLine().Split(); cnt = 0; }
		return args[cnt++];
	}

	public static int GetInt() => int.Parse(getArg());
	public static long GetLong() => long.Parse(getArg());
	public static double GetDouble() => double.Parse(getArg());
	public static decimal GetDecimal() => decimal.Parse(getArg());
	public static string GetString() => getArg();
	public static char GetChar() => getArg()[0];
	public static int[] GetInts(long N) => getArgs(N).Select(int.Parse).ToArray();
	public static long[] GetLongs(long N) => getArgs(N).Select(long.Parse).ToArray();
	public static double[] GetDoubles(long N) => getArgs(N).Select(double.Parse).ToArray();
	public static decimal[] GetDecimals(long N) => getArgs(N).Select(decimal.Parse).ToArray();
	public static string[] GetStrings(long N) => getArgs(N).ToArray();
}

public static class MyUtil
{
	public static void chmax<T>(ref T x, T y) where T : IComparable<T> { if(x.CompareTo(y) < 0) x = y; }
	public static void chmin<T>(ref T x, T y) where T : IComparable<T> { if(x.CompareTo(y) > 0)	x = y; }
	public static void swap<T>(ref T x, ref T y) { T tmp = x; x = y; y = tmp; }
	public static long rightShift(long x, long y) => (x >> (int)y);
	public static long leftShift(long x, long y) => (x << (int)y);
	public static bool contains(long s, long x) => (rightShift(s, x) & 1L) == 1L;
	public static long gcd(long x, long y) => (y == 0) ? x : gcd(y, x % y);
	public static long lcm(long x, long y) => (x / gcd(x,y)) * y;	
	public static IEnumerable<int> Range(long start, long count) => Enumerable.Range((int)start, (int)count);
	public static void Sort(this List<string> source) => source.Sort(StringComparer.Ordinal);
}


Measured memory_bytes: 78520