stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include<bits/stdc++.h>

#define FIN ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

using namespace std;

#define N 100005

#define v1 2*node

#define v2 2*node+1

#define med l+(r-l)/2

typedef long long ll;

typedef pair<int,int> pii;

vector<int> adj[N];

int n,m,res[N];

ll dp[N];

map<int,int> mapa[N];

vector<ll> st[N];

void update(int id, int node, int l, int r, int pos, int val){

    if(l == r){ st[id][node] = val%m; return; }

    if(pos <= med) update(id,v1,l,med,pos,val);

    else update(id,v2,med+1,r,pos,val);

    st[id][node] = st[id][v1]*st[id][v2]%m;

}

void upd(int u, int v, int val){

    update(u,1,0,adj[u].size()-1,mapa[u][v],val);

}

void dfs(int u, int p){

    dp[u] = 1;

    for(int v : adj[u]){

        if(v == p) continue;

        dfs(v,u);

        dp[u] = dp[u]*(1+dp[v])%m;

        upd(u,v,1+dp[v]);

    }

}

void go(int u, int p){

    res[u] = dp[u];

    for(int v : adj[u]){

        if(v == p) continue;

        upd(u,v,1);

        int ant = dp[v], cur = st[u][1]+1;

        dp[v] = dp[v]*cur%m;

        upd(v,u,cur);

        go(v,u);

        upd(v,u,1);

        dp[v] = ant;

        upd(u,v,ant+1);

    }

}

int main(){

    cin >> n >> m;

    for(int i = 0,u,v; i < n-1; i++){

        cin >> u >> v; u--,v--;

        adj[u].push_back(v);

        adj[v].push_back(u);

    }

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

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

            mapa[i][adj[i][j]] = j;

        st[i].resize(4*adj[i].size(),1);

    }

    dfs(0,-1);

    go(0,-1);

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

        cout << res[i] << endl;

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 2.7451302569892753x):
#include <bits/stdc++.h>

using namespace std;

const int MAXN=100010;

vector<int> ver[MAXN];

long long Fdp[MAXN],Sdp[MAXN],m;

int n,fa[MAXN];

 

void Fdfs(int x){

	vector<int>::iterator it;

	for(it=ver[x].begin();it!=ver[x].end();++it)

		if(*it!=fa[x]) fa[*it]=x,Fdfs(*it),Fdp[x]=Fdp[x]*(Fdp[*it]+1)%m;

	return;

}

 

void Sdfs(int x){

	int len=ver[x].size();

	long long suf[len+10],pre[len+10];

	pre[0]=1,suf[len+1]=1;

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

		pre[i]=pre[i-1]*(Fdp[ver[x][i-1]]+1)%m,suf[len-i+1]=suf[len-i+2]*(Fdp[ver[x][len-i]]+1)%m;

	for(int i=0;i!=len;++i){

		if(Sdp[ver[x][i]]!=-1) continue;

		int tmp1=Fdp[x],tmp2=Fdp[ver[x][i]];

		fa[ver[x][i]]=-1,fa[x]=ver[x][i];

		Fdp[x]=pre[i]*suf[i+2]%m,Fdp[ver[x][i]]=(Fdp[x]+1)*tmp2%m;

		Sdp[ver[x][i]]=Fdp[ver[x][i]],Sdfs(ver[x][i]);

		fa[x]=-1,fa[ver[x][i]]=x;

		Fdp[x]=tmp1,Fdp[ver[x][i]]=tmp2;

	}

	return;

}

 

int main(){

	memset(Sdp,-1,sizeof(Sdp));

	cin>>n>>m;

	fill(Fdp+1,Fdp+n+1,1);

	for(int i=1,a,b;i<n;cin>>a>>b,ver[a].push_back(b),ver[b].push_back(a),++i);

	fa[1]=-1,Fdfs(1),Sdp[1]=Fdp[1],Sdfs(1);

	for(int i=1;i<=n;++i) cout<<Sdp[i]<<endl;

	return 0;

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

#define FIN ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

using namespace std;

#define N 100005

#define v1 2*node

#define v2 2*node+1

#define med l+(r-l)/2

typedef long long ll;

typedef pair<int,int> pii;

vector<int> adj[N];

int n,m,res[N];

ll dp[N];

map<int,int> mapa[N];

vector<ll> st[N];

void update(int id, int node, int l, int r, int pos, int val){

    if(l == r){ st[id][node] = val%m; return; }

    if(pos <= med) update(id,v1,l,med,pos,val);

    else update(id,v2,med+1,r,pos,val);

    st[id][node] = st[id][v1]*st[id][v2]%m;

}

void upd(int u, int v, int val){

    update(u,1,0,adj[u].size()-1,mapa[u][v],val);

}

void dfs(int u, int p){

    dp[u] = 1;

    for(int v : adj[u]){

        if(v == p) continue;

        dfs(v,u);

        dp[u] = dp[u]*(1+dp[v])%m;

        upd(u,v,1+dp[v]);

    }

}

void go(int u, int p){

    res[u] = dp[u];

    for(int v : adj[u]){

        if(v == p) continue;

        upd(u,v,1);

        int ant = dp[v], cur = st[u][1]+1;

        dp[v] = dp[v]*cur%m;

        upd(v,u,cur);

        go(v,u);

        upd(v,u,1);

        dp[v] = ant;

        upd(u,v,ant+1);

    }

}

int main(){

    cin >> n >> m;

    for(int i = 0,u,v; i < n-1; i++){

        cin >> u >> v; u--,v--;

        adj[u].push_back(v);

        adj[v].push_back(u);

    }

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

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

            mapa[i][adj[i][j]] = j;

        st[i].resize(4*adj[i].size(),1);

    }

    dfs(0,-1);

    go(0,-1);

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

        cout << res[i] << endl;

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.3002382901472103x):
#include <bits/stdc++.h>

#define ll long long

using namespace std;

const int MAXN=1e5+10;

ll n,m,f1[MAXN],f2[MAXN],up[MAXN];

vector <ll> pre[MAXN],suf[MAXN],node[MAXN];

void dfs_dp_init(int now,int fa)

{

	f1[now]=1;

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

	{

		ll to=node[now][i];

		if(to==fa)

			continue;

		dfs_dp_init(to,now);

		f1[now]=(f1[now]*(f1[to]+1))%m;

	}

	ll sum=1;

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

	{

		ll to=node[now][i];

		if(to!=fa)

			sum=(sum*(f1[to]+1))%m;

		pre[now].push_back(sum);

	}

	sum=1;

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

	{

		ll to=node[now][i];

		if(to!=fa)

			sum=(sum*(f1[to]+1))%m;

		suf[now].push_back(sum);

	}

}

void dfs_dp(int now,int fa)

{

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

	{

		ll to=node[now][i];

		if(to==fa)

			continue;

		f2[to]=f1[to],up[to]=up[now]+1;

		if(i)

			up[to]=(up[to]*pre[now][i-1])%m;

		if(i!=node[now].size()-1)

			up[to]=(up[to]*suf[now][node[now].size()-i-2])%m;

		dfs_dp(to,now);

	}

}

int main()

{

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

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

	{

		int u,v;

		cin>>u>>v;

		node[u].push_back(v);

		node[v].push_back(u);

	}

	dfs_dp_init(1,1);

	f2[1]=f1[1];

	dfs_dp(1,1);

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

		printf("%lld\n",(f2[i]*(up[i]+1))%m);

	return 0; 

}
// 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: 1.846336220682672x):
#include <bits/stdc++.h>

#define re register

#define SIZE 100005

#define LL long long

#define VI vector<int>

using namespace std;

int n,MOD;

VI p[SIZE];

LL dp[SIZE];

LL ans[SIZE],Last[SIZE];

LL qzj[SIZE],hzj[SIZE]; 

inline void dfs(int i,int fa)

{

	for(re int j=0;j<p[i].size();++j)

		if(p[i][j]!=fa)

			dfs(p[i][j],i);

	for(re int j=0;j<p[i].size();++j)

		if(p[i][j]!=fa)

			dp[i]=(dp[i]*(dp[p[i][j]]+1))%MOD;

}

inline void HuanGen(int i,int fa)

{ 

	ans[i]=(Last[i]+1)*dp[i]%MOD;

	qzj[0]=1;hzj[p[i].size()+1]=1;

	for(re int j=0;j<p[i].size();++j)	

		if(p[i][j]!=fa)

			qzj[j+1]=qzj[j]*(dp[p[i][j]]+1)%MOD;

		else

			qzj[j+1]=qzj[j]; 

	for(re int j=p[i].size();j>=1;--j)	

		if(p[i][j-1]!=fa)

			hzj[j]=hzj[j+1]*(dp[p[i][j-1]]+1)%MOD;

		else

			hzj[j]=hzj[j+1];

	for(re int j=0;j<p[i].size();++j)	

		if(p[i][j]!=fa) 

			Last[p[i][j]]=(Last[i]+1)*qzj[j]%MOD*hzj[j+2]%MOD;

	for(re int j=0;j<p[i].size();++j)	

		if(p[i][j]!=fa) 

			HuanGen(p[i][j],i);

}

int main()

{

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

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

	{

		int x,y;

		scanf("%d%d",&x,&y);

		p[x].push_back(y);

		p[y].push_back(x);

	}

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

		dp[i]=1;

	dfs(1,0);

	HuanGen(1,0); 

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

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

	return 0;	

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

#define FIN ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

using namespace std;

#define N 100005

#define v1 2*node

#define v2 2*node+1

#define med l+(r-l)/2

typedef long long ll;

typedef pair<int,int> pii;

vector<int> adj[N];

int n,m,res[N];

ll dp[N];

map<int,int> mapa[N];

vector<ll> st[N];

void update(int id, int node, int l, int r, int pos, int val){

    if(l == r){ st[id][node] = val%m; return; }

    if(pos <= med) update(id,v1,l,med,pos,val);

    else update(id,v2,med+1,r,pos,val);

    st[id][node] = st[id][v1]*st[id][v2]%m;

}

void upd(int u, int v, int val){

    update(u,1,0,adj[u].size()-1,mapa[u][v],val);

}

void dfs(int u, int p){

    dp[u] = 1;

    for(int v : adj[u]){

        if(v == p) continue;

        dfs(v,u);

        dp[u] = dp[u]*(1+dp[v])%m;

        upd(u,v,1+dp[v]);

    }

}

void go(int u, int p){

    res[u] = dp[u];

    for(int v : adj[u]){

        if(v == p) continue;

        upd(u,v,1);

        int ant = dp[v], cur = st[u][1]+1;

        dp[v] = dp[v]*cur%m;

        upd(v,u,cur);

        go(v,u);

        upd(v,u,1);

        dp[v] = ant;

        upd(u,v,ant+1);

    }

}

int main(){

    cin >> n >> m;

    for(int i = 0,u,v; i < n-1; i++){

        cin >> u >> v; u--,v--;

        adj[u].push_back(v);

        adj[v].push_back(u);

    }

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

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

            mapa[i][adj[i][j]] = j;

        st[i].resize(4*adj[i].size(),1);

    }

    dfs(0,-1);

    go(0,-1);

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

        cout << res[i] << endl;

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 3.4257963043253845x):
#include <bits/stdc++.h>

#define re register

#define SIZE 100005

#define LL long long

#define VI vector<int>

using namespace std;

int n,MOD;

VI p[SIZE];

LL dp[SIZE];

LL ans[SIZE],Last[SIZE];

LL qzj[SIZE],hzj[SIZE]; 

inline void dfs(int i,int fa)

{

	for(re int j=0;j<p[i].size();++j)

		if(p[i][j]!=fa)

			dfs(p[i][j],i);

	for(re int j=0;j<p[i].size();++j)

		if(p[i][j]!=fa)

			dp[i]=(dp[i]*(dp[p[i][j]]+1))%MOD;

}

inline void HuanGen(int i,int fa)

{ 

	ans[i]=(Last[i]+1)*dp[i]%MOD;

	qzj[0]=1;hzj[p[i].size()+1]=1;

	for(re int j=0;j<p[i].size();++j)	

		if(p[i][j]!=fa)

			qzj[j+1]=qzj[j]*(dp[p[i][j]]+1)%MOD;

		else

			qzj[j+1]=qzj[j]; 

	for(re int j=p[i].size();j>=1;--j)	

		if(p[i][j-1]!=fa)

			hzj[j]=hzj[j+1]*(dp[p[i][j-1]]+1)%MOD;

		else

			hzj[j]=hzj[j+1];

	for(re int j=0;j<p[i].size();++j)	

		if(p[i][j]!=fa) 

			Last[p[i][j]]=(Last[i]+1)*qzj[j]%MOD*hzj[j+2]%MOD;

	for(re int j=0;j<p[i].size();++j)	

		if(p[i][j]!=fa) 

			HuanGen(p[i][j],i);

}

int main()

{

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

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

	{

		int x,y;

		scanf("%d%d",&x,&y);

		p[x].push_back(y);

		p[y].push_back(x);

	}

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

		dp[i]=1;

	dfs(1,0);

	HuanGen(1,0); 

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

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

	return 0;	

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

using namespace std;

const int MAXN=100010;

vector<int> ver[MAXN];

long long Fdp[MAXN],Sdp[MAXN],m;

int n,fa[MAXN];

 

void Fdfs(int x){

	vector<int>::iterator it;

	for(it=ver[x].begin();it!=ver[x].end();++it)

		if(*it!=fa[x]) fa[*it]=x,Fdfs(*it),Fdp[x]=Fdp[x]*(Fdp[*it]+1)%m;

	return;

}

 

void Sdfs(int x){

	int len=ver[x].size();

	long long suf[len+10],pre[len+10];

	pre[0]=1,suf[len+1]=1;

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

		pre[i]=pre[i-1]*(Fdp[ver[x][i-1]]+1)%m,suf[len-i+1]=suf[len-i+2]*(Fdp[ver[x][len-i]]+1)%m;

	for(int i=0;i!=len;++i){

		if(Sdp[ver[x][i]]!=-1) continue;

		int tmp1=Fdp[x],tmp2=Fdp[ver[x][i]];

		fa[ver[x][i]]=-1,fa[x]=ver[x][i];

		Fdp[x]=pre[i]*suf[i+2]%m,Fdp[ver[x][i]]=(Fdp[x]+1)*tmp2%m;

		Sdp[ver[x][i]]=Fdp[ver[x][i]],Sdfs(ver[x][i]);

		fa[x]=-1,fa[ver[x][i]]=x;

		Fdp[x]=tmp1,Fdp[ver[x][i]]=tmp2;

	}

	return;

}

 

int main(){

	memset(Sdp,-1,sizeof(Sdp));

	cin>>n>>m;

	fill(Fdp+1,Fdp+n+1,1);

	for(int i=1,a,b;i<n;cin>>a>>b,ver[a].push_back(b),ver[b].push_back(a),++i);

	fa[1]=-1,Fdfs(1),Sdp[1]=Fdp[1],Sdfs(1);

	for(int i=1;i<=n;++i) cout<<Sdp[i]<<endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.2479540071379456x):
#include <bits/stdc++.h>

#define re register

#define SIZE 100005

#define LL long long

#define VI vector<int>

using namespace std;

int n,MOD;

VI p[SIZE];

LL dp[SIZE];

LL ans[SIZE],Last[SIZE];

LL qzj[SIZE],hzj[SIZE]; 

inline void dfs(int i,int fa)

{

	for(re int j=0;j<p[i].size();++j)

		if(p[i][j]!=fa)

			dfs(p[i][j],i);

	for(re int j=0;j<p[i].size();++j)

		if(p[i][j]!=fa)

			dp[i]=(dp[i]*(dp[p[i][j]]+1))%MOD;

}

inline void HuanGen(int i,int fa)

{ 

	ans[i]=(Last[i]+1)*dp[i]%MOD;

	qzj[0]=1;hzj[p[i].size()+1]=1;

	for(re int j=0;j<p[i].size();++j)	

		if(p[i][j]!=fa)

			qzj[j+1]=qzj[j]*(dp[p[i][j]]+1)%MOD;

		else

			qzj[j+1]=qzj[j]; 

	for(re int j=p[i].size();j>=1;--j)	

		if(p[i][j-1]!=fa)

			hzj[j]=hzj[j+1]*(dp[p[i][j-1]]+1)%MOD;

		else

			hzj[j]=hzj[j+1];

	for(re int j=0;j<p[i].size();++j)	

		if(p[i][j]!=fa) 

			Last[p[i][j]]=(Last[i]+1)*qzj[j]%MOD*hzj[j+2]%MOD;

	for(re int j=0;j<p[i].size();++j)	

		if(p[i][j]!=fa) 

			HuanGen(p[i][j],i);

}

int main()

{

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

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

	{

		int x,y;

		scanf("%d%d",&x,&y);

		p[x].push_back(y);

		p[y].push_back(x);

	}

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

		dp[i]=1;

	dfs(1,0);

	HuanGen(1,0); 

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

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

	return 0;	

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

#define ll long long

using namespace std;

const int MAXN=1e5+10;

ll n,m,f1[MAXN],f2[MAXN],up[MAXN];

vector <ll> pre[MAXN],suf[MAXN],node[MAXN];

void dfs_dp_init(int now,int fa)

{

	f1[now]=1;

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

	{

		ll to=node[now][i];

		if(to==fa)

			continue;

		dfs_dp_init(to,now);

		f1[now]=(f1[now]*(f1[to]+1))%m;

	}

	ll sum=1;

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

	{

		ll to=node[now][i];

		if(to!=fa)

			sum=(sum*(f1[to]+1))%m;

		pre[now].push_back(sum);

	}

	sum=1;

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

	{

		ll to=node[now][i];

		if(to!=fa)

			sum=(sum*(f1[to]+1))%m;

		suf[now].push_back(sum);

	}

}

void dfs_dp(int now,int fa)

{

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

	{

		ll to=node[now][i];

		if(to==fa)

			continue;

		f2[to]=f1[to],up[to]=up[now]+1;

		if(i)

			up[to]=(up[to]*pre[now][i-1])%m;

		if(i!=node[now].size()-1)

			up[to]=(up[to]*suf[now][node[now].size()-i-2])%m;

		dfs_dp(to,now);

	}

}

int main()

{

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

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

	{

		int u,v;

		cin>>u>>v;

		node[u].push_back(v);

		node[v].push_back(u);

	}

	dfs_dp_init(1,1);

	f2[1]=f1[1];

	dfs_dp(1,1);

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

		printf("%lld\n",(f2[i]*(up[i]+1))%m);

	return 0; 

} 

// Faster C++ solution for the same problem (measured speedup: 2.6347449773514384x):
#include <bits/stdc++.h>

#define re register

#define SIZE 100005

#define LL long long

#define VI vector<int>

using namespace std;

int n,MOD;

VI p[SIZE];

LL dp[SIZE];

LL ans[SIZE],Last[SIZE];

LL qzj[SIZE],hzj[SIZE]; 

inline void dfs(int i,int fa)

{

	for(re int j=0;j<p[i].size();++j)

		if(p[i][j]!=fa)

			dfs(p[i][j],i);

	for(re int j=0;j<p[i].size();++j)

		if(p[i][j]!=fa)

			dp[i]=(dp[i]*(dp[p[i][j]]+1))%MOD;

}

inline void HuanGen(int i,int fa)

{ 

	ans[i]=(Last[i]+1)*dp[i]%MOD;

	qzj[0]=1;hzj[p[i].size()+1]=1;

	for(re int j=0;j<p[i].size();++j)	

		if(p[i][j]!=fa)

			qzj[j+1]=qzj[j]*(dp[p[i][j]]+1)%MOD;

		else

			qzj[j+1]=qzj[j]; 

	for(re int j=p[i].size();j>=1;--j)	

		if(p[i][j-1]!=fa)

			hzj[j]=hzj[j+1]*(dp[p[i][j-1]]+1)%MOD;

		else

			hzj[j]=hzj[j+1];

	for(re int j=0;j<p[i].size();++j)	

		if(p[i][j]!=fa) 

			Last[p[i][j]]=(Last[i]+1)*qzj[j]%MOD*hzj[j+2]%MOD;

	for(re int j=0;j<p[i].size();++j)	

		if(p[i][j]!=fa) 

			HuanGen(p[i][j],i);

}

int main()

{

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

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

	{

		int x,y;

		scanf("%d%d",&x,&y);

		p[x].push_back(y);

		p[y].push_back(x);

	}

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

		dp[i]=1;

	dfs(1,0);

	HuanGen(1,0); 

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

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

	return 0;	

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

#define LL long long

#define PII pair<int,int>

using namespace std;

const int MAXN = 100005;



int N, MOD, dp[MAXN], up[MAXN];

vector<int> G[MAXN], pref[MAXN], suff[MAXN];



void dfs1(int v, int p) {

//	cout << "DFS1 " << v << " " << p << "\n";

	dp[v] = 1;

	for (int to : G[v]) {

		if (to == p) continue;

		dfs1(to, v);

		dp[v] = (LL)dp[v] * (dp[to] + 1) % MOD;

	}

	

	pref[v].resize(G[v].size() + 1);

	pref[v][0] = 1;

	for (int i = 0; i < G[v].size(); i++) {

		int to = G[v][i];

		LL mul = to == p ? 1 : dp[to] + 1;

		pref[v][i + 1] = pref[v][i] * mul % MOD;

	}

	

	suff[v].resize(G[v].size() + 1);

	suff[v][G[v].size()] = 1;

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

		int to = G[v][i];

		LL mul = to == p ? 1 : dp[to] + 1;

		suff[v][i] = suff[v][i + 1] * mul % MOD;

	}

}



void dfs2(int v, int p) {

//	cout << "DFS2 " << v << " -> " << dp[v] << " " << up[v] << "\n";

	for (int i = 0; i < G[v].size(); i++) {

		int to = G[v][i];

		if (to == p) continue;

//		cout << "Set " << to << " " << i << " -> " << pref[v][i] << " " <<  suff[v][i + 1] << "\n";

		up[to] = (1LL + (LL)pref[v][i] * suff[v][i + 1] % MOD * up[v] % MOD) % MOD;

		dfs2(to, v);

	}

}



int main() {

	ios_base::sync_with_stdio(0); cin.tie(0);

	cin >> N >> MOD;

	for (int i = 0; i < N - 1; i++) {

		int x, y;

		cin >> x >> y;

		G[x].push_back(y);

		G[y].push_back(x);

	}

	

	dfs1(1, 1);

	up[1] = 1;

	dfs2(1, 1);

	

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

		cout << (LL) dp[i] * up[i] % MOD << "\n";

	}

}


// Faster C++ solution for the same problem (measured speedup: 2.611966728293414x):
#include <bits/stdc++.h>

#define PII pair<int, int>

#define LL long long

using namespace std;

const int MAXN = 100005;

const LL INF = (LL)1e9 + 5;



int N, par[MAXN];

LL MOD, dp[MAXN], up_dp[MAXN];

vector<int> G[MAXN];



void dfs1(int v, int p) {

	par[v] = p;

	dp[v] = 1;

	for (int to : G[v]) {

		if (to != p) {

			dfs1(to, v);

			dp[v] = dp[v] * dp[to] % MOD;

		}

	}

	dp[v]++;

}



void dfs2(int v, int p) {

	LL suff;

	vector<LL> pref(G[v].size() + 1);

	pref[0] = suff = 1;

	for (int i = 0; i < G[v].size(); i++) {

		LL mul = (G[v][i] == p ? 1 : dp[G[v][i]]);

		pref[i + 1] = pref[i] * mul % MOD;

	}

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

		if (G[v][i] != p) {

			up_dp[G[v][i]] = (suff * pref[i] % MOD * up_dp[v] + 1)% MOD;

			suff = suff * dp[G[v][i]] % MOD;

			dfs2(G[v][i], v);

		}

	}

}



int main() {

	ios_base::sync_with_stdio(0); cin.tie(0);



	cin >> N >> MOD;

	for (int i = 0; i < N - 1; i++) {

		int x, y;

		cin >> x >> y;

		G[x].push_back(y);

		G[y].push_back(x);

	}

	

	dfs1(1, 1);

	up_dp[1] = 1;

	dfs2(1, 1);

	

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

		LL ans = up_dp[i];

		for (int to : G[i]) {

			if (to != par[i]) {

				ans = ans * dp[to] % MOD;

			}

		}

		cout << ans << "\n";

	}

	

	return 0;

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

#define ull unsigned long long

#define ll long long

#define endl '\n'

#define pb push_back

#define mod 1000000007

#define mp make_pair

#define pi acos(-1)

#define line cout<<"----------------------------"<<endl

#define fast ios_base::sync_with_stdio(false); cin.tie(NULL)

#define filein freopen("input.txt","r",stdin)

#define fileout freopen("output.txt","w",stdout)

using namespace std;

const ll mx=200009;	

vector<int> v[mx]; 

ll ans[mx];

int n,m; 

ll upor[mx]; 

void dfs(int s, int par)

{

	ans[s]=1;

	for(auto x: v[s])

	{

		if(x==par) continue; 

		dfs(x,s);

		ans[s]*=ans[x]+1;

		ans[s]%=m;

	}

}

void dfs1(int s, int par)

{

	vector<ll> pre((int)v[s].size()+5),suf((int)v[s].size()+5);

	pre[0]=1; 

	for(int i=1;i<=(int)v[s].size();i++)

	{

		int x=v[s][i-1] ;

		pre[i] =pre[i-1];

		if(x==par) continue; 

		pre[i]*=(ans[x]+1); 

		pre[i]%=m; 

	}

	suf[(int)v[s].size()+1]=1; 

	for(int i=(int)v[s].size();i>0;i--)

	{

		suf[i]=suf[i+1] ; 

		int x=v[s][i-1] ; 

		if(x==par) continue; 

		suf[i]*=(ans[x]+1); 

		suf[i]%=m; 

	}

	upor[1]=1; 

	for(int i=1;i<=(int)v[s].size();i++)

	{

		int x=v[s][i-1]; 

		if(x==par) continue; 

		upor[x]=1; 

		upor[x]=(upor[x]*pre[i-1])%m; 

		upor[x]=(upor[x]*suf[i+1])%m; 

		upor[x]*=upor[s];  

		upor[x]++; 

		upor[x]%=m; 

		dfs1(x,s);

	}

}

int main()

{

	

	cin>>n>>m;

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

	{

		ll a,b; 

		cin>>a>>b;

		v[a].pb(b);

		v[b].pb(a);

	}

	dfs(1,-1);

	dfs1(1,-1);

	upor[1]=1; 

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

	{

		ans[i]=(ans[i]*upor[i])%m; 

		cout<<ans[i]<<endl; 

	}

}


// Faster C++ solution for the same problem (measured speedup: 1.8170420859560215x):
#include <bits/stdc++.h>



#define pb push_back

using namespace std;

typedef long long ll;

const int N = 100007;

vector<int> e[N];



ll f[N];

ll ans[N];

int m;



void solve(int u, int p = -1) {



	f[u] = 1;

	for(int v : e[u]) {

		if(v == p) continue;

		solve(v, u);



		f[u] = f[u] * f[v] % m;

	}



	f[u] = (f[u] + 1)%m;

}



void dfs(int u, int p = -1) {



	if(p != -1) { // adiciona f do pai

		f[u] = (f[u] - 1 + m)%m;

		f[u] = f[u] * f[p] % m;

		f[u] = (f[u] + 1)%m;

	}



	ans[u] = (f[u] - 1 + m)%m;



	vector<ll> suf(e[u].size() + 1, 1);

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

		suf[i] = suf[i+1] * f[e[u][i]] % m;

	}



	ll aux = 1;

	for(int i = 0; i < e[u].size(); i++) {

		int v = e[u][i];



		ll valor = f[v]; // valor antes de mudar, pra nao precisar remover o pai

		if(v != p) {

			// remove v

			f[u] = aux * suf[i + 1] % m;

			f[u] = (f[u] + 1)%m; 



			dfs(v, u);	

		}

		

		aux = aux * valor % m;

	}

}



int main() {

	ios::sync_with_stdio(0); cin.tie(0);



	int n; cin >> n >> m;



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

		int u, v; cin >> u >> v;

		e[u].pb(v);

		e[v].pb(u);

	}



	solve(1, -1);

	dfs(1, -1);



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

		cout << ans[i] << '\n';

	}

}