stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include<cstdio>

#include<cmath>

#include<iostream>

#include<algorithm>

#include<cstring>

#include<cstdlib>

#include<queue>

#include<map>

#include<stack>

#include<set>

#include<vector>

#include<functional>

using namespace std;

typedef long long LL;

inline LL read()

{

	LL kk=0,f=1;

	char cc=getchar();

	while(cc<'0'||cc>'9'){if(cc=='-')f=-1;cc=getchar();}

	while(cc>='0'&&cc<='9'){kk=(kk<<1)+(kk<<3)+cc-'0';cc=getchar();}

	return kk*f;

}

LL cou=0,asd=0,len,k;

void dfs(LL now,int de)

{

	if(de==len)

	{

		cou++;

		if(cou==k)asd=now;

		return;

	}

	LL per=now%10;

	for(LL i=max(0ll,per-1);i<=min(9ll,per+1);++i)

	{

		dfs(now*10+i,de+1);

	}

}

int main()

{

	k=read();

	for(LL i=1;i<=10;++i)

	for(LL j=1;j<=9;++j)

	{

		len=i;

		dfs(j,1);

		if(cou>k)break;

	}

	printf("%lld\n",asd);

}

// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

using namespace std;

int k;

int sum=9;

int a[11];

void sxqn(int n,int m,int p)

{

	if(m<0)return;

	if(m>9)return;

	a[p]=m;

	if(p==n)

	{

	  sum++;

	  if(sum==k)

	  {

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

	  	  cout<<a[i];

	  	exit(0);

	  }

	  return;

	}

	sxqn(n,m-1,p+1);

	sxqn(n,m,p+1);

	sxqn(n,m+1,p+1);

}

int main()

{

	cin>>k;

	if(k<10)

	{

	  cout<<k;

	  return 0;

	}

	for(int i=2;i<=10;i++)

	  for(int j=1;j<=9;j++)

	    sxqn(i,j,1);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>



#define rep(i,n) for(int i=0;i<n;i++)

#define repn(i,n) for(int i=1;i<=n;i++)

#define LL long long

#define pii pair <int,int>

#define fi first

#define se second

#define pb push_back

#define mpr make_pair



using namespace std;



const LL MOD=1e9+7;



LL n,lb=1,ub=1e18,mid,dp[20][20][2][2];



string tostr(LL x)

{

	string ret="";

	while(x>0)

	{

		ret=(char)(x%10+'0')+ret;

		x/=10;

	}

	return ret;

}



bool check()

{

	string s=tostr(mid);

	memset(dp,0,sizeof(dp));

	dp[1][0][1][0]=1;

	repn(i,s[0]-'0'-1) dp[1][i][1][1]=1;

	dp[1][s[0]-'0'][0][1]=1;

	LL ret=0;

	repn(i,s.size()-1)

	{

		rep(j,10)

		{

			rep(k,2)

			{

				rep(p,2)

				{

					if(dp[i][j][k][p]==0) continue;

					if(p==1)

					{

						if(k==0)

						{

							if(abs(s[i]-'0'-j)<2) dp[i+1][s[i]-'0'][0][p|(s[i]-'0'>0)]+=dp[i][j][k][p];

							rep(r,s[i]-'0') if(abs(r-j)<2) dp[i+1][r][1][p|(r>0)]+=dp[i][j][k][p];

						}

						else rep(r,10) if(abs(r-j)<2) dp[i+1][r][1][p|(r>0)]+=dp[i][j][k][p];

					}

					else

					{

						dp[i+1][0][1][0]+=dp[i][j][k][p];

						repn(r,9) dp[i+1][r][1][1]+=dp[i][j][k][p];

					}

				}

			}

		}

	}

	rep(i,10) ret+=dp[s.size()][i][1][1];

	return ret<n;

}



int main()

{

	cin>>n;

	while(lb<ub)

	{

		mid=(lb+ub+1)/2;

		if(check()) lb=mid;

		else ub=mid-1;

	}

	cout<<lb<<endl;

	return 0;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>



#define rep(i,n) for(int i=0;i<n;i++)

#define repn(i,n) for(int i=1;i<=n;i++)

#define LL long long

#define pii pair <int,int>

#define fi first

#define se second

#define pb push_back

#define mpr make_pair



using namespace std;



const LL MOD=1e9+7;



LL n,dp[20][20];

string ans="";



LL count(LL len)

{

	memset(dp,0,sizeof(dp));

	repn(i,9) dp[0][i]=1;

	rep(i,len-1)

	{

		rep(j,10)

		{

			if(dp[i][j]==0) continue;

			if(j+1<=9) dp[i+1][j+1]+=dp[i][j];

			dp[i+1][j]+=dp[i][j];

			if(j-1>=0) dp[i+1][j-1]+=dp[i][j];

		}

	}

	LL ret=0;

	rep(i,10) ret+=dp[len-1][i];

	return ret;

}



LL conditionalCount(LL fir,LL len)

{

	memset(dp,0,sizeof(dp));

	dp[0][fir]=1;

	rep(i,len-1)

	{

		rep(j,10)

		{

			if(dp[i][j]==0) continue;

			if(j+1<=9) dp[i+1][j+1]+=dp[i][j];

			dp[i+1][j]+=dp[i][j];

			if(j-1>=0) dp[i+1][j-1]+=dp[i][j];

		}

	}

	LL ret=0;

	rep(i,10) ret+=dp[len-1][i];

	return ret;

}



int main()

{

	cin>>n;

	LL len,tmp;

	repn(i,15)

	{

		tmp=count(i);

		if(tmp>=n)

		{

			len=i;

			break;

		}

		n-=tmp;

	}

	for(int i=len;i>0;i--)

	{

		for(int j=(i==len ? 1:ans[ans.size()-1]-'0'-1);j<(i==len ? 10:ans[ans.size()-1]-'0'+2);j++)

		{

			tmp=conditionalCount(j,i);

			if(tmp>=n)

			{

				ans.pb(j+'0');

				break;

			}

			n-=tmp;

		}

	}

	cout<<ans<<endl;

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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];

vector<int> in[MAXN];



void dfs1(int u, int fa)

{

	int cnt = 0;

	f[u]    = 1;

	in[u].push_back(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));

		cnt++;

		in[u].push_back(v);

	}

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

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

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

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

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

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

		t[in[u][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;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;

using ll=long long;

using pii=pair<int,int>;

#define F first

#define S second

#define sz(x) int(x.size())

#define pb push_back



int mod,n;

vector<vector<int>> G;

vector<ll> res,ans;

void dfs1(int u,int fa){

    res[u]=1;

    for(auto to:G[u]){

        if(to==fa) continue;

        dfs1(to,u);

        (res[u]*=ll(res[to]+1))%=mod;

    }

}

void dfs2(int root,int fa,ll now){

    ans[root]=((now+1)*res[root])%mod;

    vector<pair<ll,int>> pre,suf;

    for(auto to:G[root]){

        if(to==fa) continue;

        pre.pb({res[to]+1,to});

        suf.pb({res[to]+1,to});

    }

    for(int i=1;i<sz(pre);i++) (pre[i].F*=ll(pre[i-1].F))%=mod;

    for(int i=sz(suf)-2;i>=0;i--) (suf[i].F*=ll(suf[i+1].F))%=mod;

    for(int i=0;i<sz(pre);i++){

        ll l=(i==0?1:pre[i-1].F),r=(i==sz(pre)-1?1:suf[i+1].F);

        dfs2(pre[i].S,root,l*r%mod*(now+1)%mod);

    }

}

int main() {

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

    cin>>n>>mod;

    G.resize(n+1);

    res.resize(n+1);

    ans.resize(n+1);

    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,0);

    dfs2(1,0,0);

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

        cout<<ans[i]<<endl;

    }

    return 0;

}




// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

#define inp_out_work ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

#define testCase    int T; cin>>T; while(T--)

#define fr(i,n) for(int i=0;i<n;i++)

#define fro(i,s,e) for(int i=s;i<=e;i++)

#define endl '\n'

#define debug(P) cout<<#P<<" = "<<P<<endl;

#define pb push_back

#define FILE freopen("in.txt","r",stdin); freopen("out.txt","w",stdout);





using namespace std;



typedef long long ll;



const ll MOD = 1e9 + 7;

const int NAX = 100001;



vector<int> adj[NAX];

vector<ll> pref[NAX], suff[NAX];

int n, M;



ll dp1[NAX]; // no. of subtrees starting from this node toward downward

ll dp2[NAX]; // no. of subtrees starting from this node toward upward



// calculate dp1

void dfs1(int x, int p){

    dp1[x] = 1;

    for(int c : adj[x]){

        if(c!=p){

            dfs1(c, x);

            dp1[x] = (dp1[x]*(1+dp1[c]))%M;

            suff[x].pb(1+dp1[c]);

            pref[x].pb(dp1[x]);

        }

    }

    for(int i = suff[x].size()-2; i >= 0; i--){

        suff[x][i] = (suff[x][i] * suff[x][i+1])%M;

    }

}



void dfs2(int x, int p){

    int j = 0;

    for(int c : adj[x]){

        if(c != p){

            ll val = 1;

            if(j>0)val = (val * pref[x][j-1])%M;

            if(j+1<suff[x].size())val = (val * suff[x][j+1])%M;

            ++j;

            dp2[c] = (1 + dp2[x]*val)%M;

            dfs2(c,x);

        }

    }



}



int main(){

    cin>>n>>M;

    fr(i,n-1){

        int u, v;

        cin>>u>>v;

        adj[u].pb(v);

        adj[v].pb(u);

    }



    dfs1(1, 0);

    dp2[1] = 1;

    dfs2(1,0);



    fro(i,1,n){

        cout<< dp1[i]*dp2[i]%M << endl;

    }



}


// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

#define inp_out_work ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

#define testCase    int T; cin>>T; while(T--)

#define endl '\n'

#define debug(P) cout<<#P<<" = "<<P<<endl;

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



using namespace std;



const int MAXN = 100001;

const int mod = 1000000007;



typedef long long ll;



int n, m;

vector<int> adj[MAXN];



ll dp1[MAXN];

ll dp2[MAXN];



void dfs1(int x, int p){

	dp1[x] = 1;

	for(int c : adj[x]){

		if(c != p){

			dfs1(c, x);

			dp1[x] = dp1[x] * (dp1[c]+1)%m;

		}

	}

}



void dfs2(int x, int p){



	vector<ll> sf;

	ll mul = 1;

	sf.push_back(mul);



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

		if(adj[x][i] != p){

			mul = (mul * (1+dp1[adj[x][i]]))%m;

			sf.push_back(mul);

		}

	}



	mul = 1;



	for(int c : adj[x]){

		if(c != p){

			ll tt = (mul * sf[sf.size()-2])%m;

			dp2[c] = (tt * dp2[x] + 1)%m;

			mul = mul * (1+dp1[c]) % m;

			sf.pop_back();

			dfs2(c, x);

		}

	}

}





int main(){ 

	cin>>n>>m;



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

		int u, v;

		cin>>u>>v;

		adj[u].push_back(v);

		adj[v].push_back(u);

	}



	dp2[1] = 1;



	dfs1(1, 0);

	dfs2(1, 0);



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

		cout<< dp1[i] * dp2[i] %m << endl;

	}



}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>



#define mp make_pair



using namespace std;



typedef long long ll;



// uniform_int_distribution<int>

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());



vector<int> G[100005];

int dp[100005];

int ans[100005];

map<int,int> tab;

vector<int> ps[100005], ss[100005];

int m;



void mult(int &a, int b) {

	a = ((ll)a*b)%m;

}



int dfs(int v, int p) {

	dp[v] = 1;

	vector<int> childs;



	for(int x : G[v])

		if(x != p) {

			mult(dp[v], 1+dfs(x,v));

			tab[x] = childs.size();

			childs.push_back(x);

			ps[v].push_back(dp[v]);



		}



	int aux = 1;

	for(int i = (int)childs.size()-1; i >= 0; i--) {

		int x = childs[i];

		mult(aux, 1+dp[x]);

		ss[v].push_back(aux);

	}



	return dp[v];

}



void dfs2(int v, int p) {

	ans[v] = dp[v];

	if(v != p) {

		int idx = tab[v];

		

		int prefVal = idx ? ps[p][idx-1] : 1;

		int sufVal = (int)ss[p].size()-2-idx < 0 ? 1 : ss[p][(int)ss[p].size()-2-idx];



		mult(prefVal, sufVal);

		

		int tot = (1+prefVal)%m;

		ans[v] = ((ll)ans[v]*tot)%m;

		ps[v].push_back(ans[v]);

		

		ss[v].insert(ss[v].begin(), 1);

		

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

			mult(ss[v][i], tot);

	}



	for(int x : G[v])

		if(x != p)

			dfs2(x,v);

}



int main() {

	int n,a,b;

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



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

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

		G[a].push_back(b);

		G[b].push_back(a);

	}



	dfs(1,1);

	dfs2(1,1);



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

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



    return 0;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

 

#define ff first

#define ss second

#define mp make_pair

 

using namespace std;



typedef long long ll;



vector<int> G[100005];

int dp[100005], MOD, ans[100005], father[100005];



void f(int v, int p) {

	dp[v] = 1;



	for(int u : G[v]) {

		if(u == p) continue;

		f(u, v);

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

	}

}



void g(int v, int p) {

	vector<int> pre, suf;

	int cur = 1;

	for(int u : G[v]) {

		if(u == p) {

			pre.push_back(cur);

			continue;

		}

		cur = 1LL*cur*(1+dp[u])%MOD;

		pre.push_back(cur);

	}



	cur = 1;

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

		int u = G[v][i];

		if(u == p) {

			suf.push_back(cur);

			continue;

		}

		cur = 1LL*cur*(1+dp[u])%MOD;

		suf.push_back(cur);

	}

	reverse(suf.begin(), suf.end());



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

		int u = G[v][i];

		if(u == p) continue;

		father[v] = v == 1 ? 1 : 1+father[p];



		if(i) father[v] = 1LL*father[v]*pre[i-1]%MOD;

		if(i < (int)G[v].size()-1) father[v] = 1LL*father[v]*suf[i+1]%MOD;



		ans[u] = 1LL*dp[u]*(1+father[v])%MOD;

		g(u, v);

	}

}



int main(){

	int n,a,b;



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



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

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

		G[a].push_back(b);

		G[b].push_back(a);

	}



	f(1,1);

	ans[1] = dp[1];

	g(1,1);



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

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



	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}


// Optimized solution to the same problem (faster):
#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;	

} 

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;	

} 

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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; 

} 

// Optimized solution to the same problem (faster):
#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;	

} 

// Fastest known solution for this problem: