Problem:
Score : 100 points

Problem Statement

There is a tree with N vertices, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N - 1), the i-th edge connects Vertex x_i and y_i.

Taro has decided to paint each vertex in white or black, so that any black vertex can be reached from any other black vertex by passing through only black vertices.

You are given a positive integer M.
For each v (1 \leq v \leq N), answer the following question:

Assuming that Vertex v has to be black, find the number of ways in which the vertices can be painted, modulo M.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

2 \leq M \leq 10^9

1 \leq x_i, y_i \leq N

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N M
x_1 y_1
x_2 y_2
:
x_{N - 1} y_{N - 1}

Output

Print N lines.
The v-th (1 \leq v \leq N) line should contain the answer to the following question:

Assuming that Vertex v has to be black, find the number of ways in which the vertices can be painted, modulo M.

Sample Input 1

3 100
1 2
2 3

Sample Output 1

3
4
3

There are seven ways to paint the vertices, as shown in the figure below.
Among them, there are three ways such that Vertex 1 is black, four ways such that Vertex 2 is black and three ways such that Vertex 3 is black.

Sample Input 2

4 100
1 2
1 3
1 4

Sample Output 2

8
5
5
5

Sample Input 3

1 100

Sample Output 3

1

Sample Input 4

10 2
8 5
10 8
6 5
1 5
4 8
2 10
3 6
9 2
1 7

Sample Output 4

0
0
1
1
1
0
1
0
1
1

Be sure to print the answers modulo M.

Solution in C# (.NET Core 3.1.201) (judge verdict: Runtime Error)

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;
		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;}
}