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# (Mono 4.6.2.0) (judge verdict: Time Limit Exceeded)

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
using static System.Math;
class Z { static void Main() => new K(); }
class K
{
	int F => int.Parse(Str);
	long FL => long.Parse(Str);
	int[] G => Strs.Select(int.Parse).ToArray();
	long[] GL => Strs.Select(long.Parse).ToArray();
	double[] GR => Strs.Select(double.Parse).ToArray();
	string Str => ReadLine();
	string[] Strs => Str.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
	// public const int MOD = 1000000007;
	public const long INFL = 4011686018427387913L;
	public K()
	{
		SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false });
		Solve();
		Out.Flush();
	}
	int V, Mod;
	HashSet<int>[] es;
	int[] par;
	long[] dp0, dp1;
	bool[] done0, done1;
	void Solve()
	{
		var I = G;
		V = I[0];
		Mod = I[1];
		SegmentTree.Mod = Mod;
		es = new HashSet<int>[V];
		par = new int[V];
		dp0 = new long[V];
		dp1 = new long[V];
		done0 = new bool[V];
		done1 = new bool[V];
		for (var u = 0; u < V; u++) es[u] = new HashSet<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);
		}
		DFS(0, -1);
		for (var u = 0; u < V; u++) Calc0(u);
		//for (var u = 1; u < V; u++) Calc1(u);
		DFS2(0);
		for (var u = 0; u < V; u++) WriteLine(Calc0(u) * (dp1[u] + 1) % Mod);
	}
	// dp0[u]=(uを黒, par[u]を白に塗る場合の数)
	// dp1[u]=(uを白, par[u]を黒に塗る場合の数)
	long Calc0(int u)
	{
		if (!done0[u])
		{
			done0[u] = true;
			dp0[u] = 1L;
			foreach (var v in es[u]) dp0[u] = dp0[u] * (Calc0(v) + 1) % Mod;
		}
		return dp0[u];
	}
	/*long Calc1(int u)
	{
		if (u == 0) return 0;
		if (!done1[u])
		{
			done1[u] = true;
			dp1[u] = (Calc1(par[u]) + 1) % Mod;
			foreach (var v in es[par[u]]) if (v != u) dp1[u] = dp1[u] * (Calc0(v) + 1) % Mod;
		}
		return dp1[u];
	}*/
	void DFS2(int u)
	{
		var t = new List<long>();
		var cs = es[u].ToList();
		foreach (var v in cs) t.Add(Calc0(v) + 1);
		var seg = new SegmentTree(t);
		for (var i = 0; i < cs.Count; i++) dp1[cs[i]] = (dp1[u] + 1) * seg.Product(0, i) % Mod * seg.Product(i + 1, cs.Count) % Mod;
		foreach (var v in cs) DFS2(v);
	}
	void DFS(int u, int p)
	{
		par[u] = p;
		es[u].Remove(p);
		foreach (var v in es[u]) DFS(v, u);
	}
}
class SegmentTree
{
	public static int Mod;
	readonly int N2;
	readonly long[] segtree;
	public SegmentTree(int N) : this(new long[N]) { }
	public SegmentTree(IList<long> array)
	{
		N2 = 1;
		while (N2 < array.Count) N2 <<= 1;
		segtree = new long[2 * N2 - 1];
		for (var i = 0; i < 2 * N2 - 1; i++) segtree[i] = 1;
		for (var i = 0; i < array.Count; i++) segtree[i + N2 - 1] = array[i] % Mod;
		for (var i = N2 - 2; i >= 0; i--) segtree[i] = segtree[2 * i + 1] * segtree[2 * i + 2] % Mod;
	}
	public long Product(int from, int to) => Product(from, to, 0, 0, N2);
	long Product(int from, int to, int node, int l, int r)
	{
		if (to <= l || r <= from) return 1;
		if (from <= l && r <= to) return segtree[node];
		return Product(from, to, 2 * node + 1, l, (l + r) >> 1) * Product(from, to, 2 * node + 2, (l + r) >> 1, r) % Mod;
	}
}