using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using static System.Math;
using System.Numerics;
using vt=System.ValueTuple<int,int>;
static class Program{
	const int mod=(int)1e9+7;
	static List<vt>[] li;
	static bool[] b;
	static int[] pa;
	static long[] dp;
	static long[][] dpl,dpr;
	static int p;
	const int ie=1;
	static void Main(){
		Sc sc=new Sc();
		var s=sc.Ia;
		int n=s[0],m=n-1;
		if(n==1){
			Console.WriteLine(1);
			return;
		}
		p=s[1];
		li=new List<vt>[n+1];
		b=new bool[n+1];
		dp=new long[n+1];
		dpl=new long[n+1][];
		dpr=new long[n+1][];
		pa=new int[n+1];
		for(int i=1;i<=n;i++){
			li[i]=new List<vt>();
			li[i].Add((-1,-1));
		}
		for(int i=0;i<m;i++){
			var e=sc.Ia;
			li[e[0]].Add((e[1],1));
			li[e[1]].Add((e[0],1));
		}
		int o=1;
		Fu1(o,0);
		Fu2(o,li[o][1].Item2);
		StringBuilder sb=new StringBuilder();
		for(int i = 1;i<=n;i++) {sb.Append(dp[i]+"\n");}
		Console.Write(sb);
	}
	static void Fu2(int a,long g){
		dp[a]=Fop(dp[a],g);
		for(int i=1;i<li[a].Count;i++){
			if(li[a][i].Item1!=pa[a]){
				Fu2(li[a][i].Item1,Fop(Fop(dpl[a][i-1],dpr[a][i+1]),g)+li[a][i].Item2);
			}
		}
	}
	static void Fu1(int a,int o){
		b[a]=true;
		pa[a]=o;
		dpl[a]=new long[li[a].Count];
		dpr[a]=new long[li[a].Count+1];
		dpl[a][0]=dpr[a][li[a].Count]=ie;
		for(int i=1;i<li[a].Count;i++){
			if(!b[li[a][i].Item1]){
				Fu1(li[a][i].Item1,a);
				dpl[a][i]=dpr[a][i]=dp[li[a][i].Item1]+li[a][i].Item2;
			}
			else{
				dpl[a][i]=dpr[a][i]=ie;
			}
			dpl[a][i]=Fop(dpl[a][i],dpl[a][i-1]);
		}
		for(int i = li[a].Count-1;i>=0;i--) {
			dpr[a][i]=Fop(dpr[a][i],dpr[a][i+1]);
		}
		dp[a]=dpl[a][li[a].Count-1];
	}
	static long Fop(long a,long b){
		return a*b%p;
	}
}
public class Sc{
	public int I{get{return int.Parse(Console.ReadLine());}}
	public long L{get{return long.Parse(Console.ReadLine());}}
	public double D{get{return double.Parse(Console.ReadLine());}}
	public string S{get{return Console.ReadLine();}}
	public int[] Ia{get{return Array.ConvertAll(Console.ReadLine().Split(),int.Parse);}}
	public long[] La{get{return Array.ConvertAll(Console.ReadLine().Split(),long.Parse);}}
	public double[] Da{get{return Array.ConvertAll(Console.ReadLine().Split(),double.Parse);}}
	public string[] Sa{get{return Console.ReadLine().Split();}}
	public object[] Oa{get{return Console.ReadLine().Split();}}
	public int[] Ia2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),int.Parse);}}
	public int[] Ia3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),int.Parse);}
	public long[] La2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),long.Parse);}}
	public long[] La3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),long.Parse);}
	public double[] Da2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),double.Parse);}}
	public double[] Da3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),double.Parse);}
	public T[] Arr<T>(int n,Func<T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f();}return a;}
	public T[] Arr<T>(int n,Func<int,T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i);}return a;}
	public T[] Arr<T>(int n,Func<string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(Console.ReadLine().Split());}return a;}
	public T[] Arr<T>(int n,Func<int,string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i,Console.ReadLine().Split());}return a;}
}

Measured memory_bytes: 71000