// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

#define all(v) ((v).begin()),((v).end())

#define ll long long

#define fastIO cout << fixed << setprecision(12), ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)

double const EPS = 1e-10, PI = acos(-1);

const int N = 2e5 + 9, M = 1e5 + 9, OO = 1e9 + 7, MOD = 1e9 + 7;

const ll inf = 1e18;



int n, mod;

vector<int> adj[N];

ll up[N], down[N];



ll mul(ll a, ll b) {

  return (a * b) % mod;

}



void dfsDown(int node, int par) {

  ll ret = 1;

  for(int it: adj[node]) {

    if(it == par) continue;

    dfsDown(it, node);

    ret = mul(ret, down[it] + 1);

  }

  down[node] = ret;

}



void dfsUp(int node, int par, ll cur) {

  up[node] = cur;

  vector<ll> suf(adj[node].size(), 1);

  ll ret = 1;

  for (int i = ((int)adj[node].size()) - 1; i >= 0; --i) {

    suf[i] = ret;

    int child = adj[node][i];

    if(par == child) continue;

    ret = mul(ret, (down[child] + 1));

  }

  ret = 1;

  for (int i = 0; i < (int)adj[node].size(); ++i) {

    int child = adj[node][i];

    if(par == child) continue;



    dfsUp(child, node, mul(mul(ret, suf[i]), cur) + 1); // . . .   if you want to calc for mid(.) so you need val of suf ( (x + 1) to n ) and all before you (1 to (x-1))



    ret = mul(ret, down[child] + 1);

  }

}



int main() {

  fastIO;

#ifdef LOCAL

  freopen("input.in", "rt", stdin);

#endif



  cin >> n >> mod;

  int u, v;

  for (int i = 1; i < n; ++i) {

    cin >> u >> v;

    adj[u].push_back(v);

    adj[v].push_back(u);

  }

  dfsDown(1, -1);

  dfsUp(1, -1, 1);

  for (int i = 1; i <= n; ++i) {

//    cerr << down[i] << ' ' << up[i] << endl;

    cout << mul(up[i], down[i]) << '\n';

  }

  return 0;

}


// Faster C++ solution for the same problem (measured speedup: 2.509293753545788x):
#include <cstdio>

#include <iostream>

#include <vector>

#include <algorithm>

#include <cstring>



using namespace std;



const int MAXN = 100010;

typedef long long ll;



int n, m;



inline int add(int x, int y){return (x+=y)>=m?x-m:x;}

inline int mul(int x, int y){return 1ll*x*y%m;}



struct edge{

	int next, to;

	edge(int N=0,int T=0):next(N),to(T){}

}e[MAXN<<1];

int fir[MAXN], num = 0;

inline void join(int a, int b)

{

	e[++num].next = fir[a];

	fir[a] = num;

	e[num].to = b;

}



int f[MAXN], g[MAXN], ans[MAXN], pre[MAXN], suf[MAXN], t[MAXN], in[MAXN]; 



void dfs1(int u, int fa)

{

	int cnt = 0;

	f[u]    = 1;

	for(int i=fir[u]; i; i=e[i].next)

	{

		int v = e[i].to;

		if(v == fa) continue;

		dfs1(v, u);

		f[u] = mul(f[u], add(f[v], 1));

	}

	for(int i=fir[u];i;i=e[i].next)

		if(e[i].to!=fa)in[++cnt] = e[i].to;

	pre[0] = suf[cnt+1] = 1;

	for(int i=1; i<=cnt; i++)

		pre[i] = mul(pre[i-1], add(f[in[i]], 1));

	for(int i=cnt; i>=1; i--)

		suf[i] = mul(suf[i+1], add(f[in[i]], 1));

	for(int i=1; i<=cnt; i++)

		t[in[i]] = mul(pre[i-1], suf[i+1]);

}

void dfs2(int u, int fa)

{

	int cnt = 0;

	for(int i=fir[u]; i; i=e[i].next)

	{

		int v = e[i].to;

		if(v == fa)continue;

		++cnt;

		g[v] = add(mul(g[u], t[v]), 1);

		ans[v] = mul(g[v], f[v]);

		dfs2(v, u);

	}

}

int main()

{

	scanf("%d%d",&n,&m);

	for(int i=1,a,b;i<n;i++)

		scanf("%d%d",&a,&b), join(a, b), join(b, a);

	dfs1(1, 0);

	g[1] = 1; ans[1] = f[1];

	dfs2(1, 0);

	for(int i=1;i<=n;i++)

		printf("%d\n" ,ans[i]);

	return 0;

}