stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#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);

}

// Faster C++ solution for the same problem (measured speedup: 1.1424347167419262x):
#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;

}
// Slower C++ solution:
#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);

}

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

using namespace std;

queue<unsigned long long>q;

unsigned long long k;

void BFS() {

	while ( ! q.empty() ) {

		int u=q.front()%10;

		if (u-1>=0) {

			k--;

			q.push(q.front()*10+u-1);

		}

		if (k==0) {

			cout<<q.back();

			return;

		}

		k--;

		q.push(q.front()*10+u);

		if (k==0) {

			cout<<q.back();

			return;

		}

		if (u+1<=9) {

			k--;

			q.push(q.front()*10+u+1);

		}

		if (k==0) {

			cout<<q.back();

			return;

		}

		q.pop();

	}

}

int main() {

	cin>>k;

	if (k<=9) {

		cout<<k;

		return 0;

	}

	k=k-9;

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

		q.push(i);

	}

	BFS();

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 1.1555893577641383x):
#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;

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 1.1133296237453085x):
#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][2];



string Itoa(LL x)

{

	string ret="";

	while(x>0)

	{

		ret.pb(x%10+'0');

		x/=10;

	}

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

	return ret;

}



bool check(LL x)

{

	string t=Itoa(x);

	LL ret=0;

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

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

	rep(i,t.size()-1)

	{

		rep(j,10)

		{

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

			ret+=dp[i][j][0];

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

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

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

		}

	}

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

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

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

	rep(i,t.size()-1)

	{

		rep(j,10)

		{

			rep(k,2)

			{

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

				if(j-1>=0&& !(k==0&&j-1>t[i+1]-'0')) dp[i+1][j-1][k|(j-1<t[i+1]-'0')]+=dp[i][j][k];

				if(!(k==0&&j>t[i+1]-'0')) dp[i+1][j][k|(j<t[i+1]-'0')]+=dp[i][j][k];

				if(j+1>=0&& !(k==0&&j+1>t[i+1]-'0')) dp[i+1][j+1][k|(j+1<t[i+1]-'0')]+=dp[i][j][k];

			}

		}

	}

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

	return ret<n;

}



int main()

{

	cin>>n;

	LL lb=1,ub=10000000000,mid;

	while(lb<ub)

	{

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

		if(check(mid)) lb=mid;

		else ub=mid-1;

	}

	cout<<lb<<endl;

	return 0;

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 5.074842131241336x):
#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;

}
// Slower C++ solution:
#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;

}

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

#define REP(x,y,z) for(int x=y;x<=z;x++)

#define MSET(x,y) memset(x,y,sizeof(x))

#define M 100005

using namespace std;

using LL = long long;

int n,m;

LL down[M], ans[M];

vector<int> e[M];

void dfs1(int cur,int fa) {

    down[cur] = 1;

    for (int i: e[cur]) if (i!=fa) {

        dfs1(i, cur);

        down[cur] = down[cur] * (down[i]+1) % m;

    }

}

void dfs2(int cur,int fa,LL up) {

    ans[cur] = down[cur] * (up+1) % m;

    

    int sz = (int)e[cur].size();

    vector<LL> fr, bk;

    fr = vector<LL> (sz);

    bk = vector<LL> (sz);

    REP(i,0,sz-1)               fr[i] = (i>0 ? fr[i-1] : 1LL)    * (e[cur][i]==fa ? (up+1) : (down[e[cur][i]]+1)) % m;

    for (int i=sz-1; i>=0; i--) bk[i] = (i<sz-1 ? bk[i+1] : 1LL) * (e[cur][i]==fa ? (up+1) : (down[e[cur][i]]+1)) % m;

    

    REP(i,0,sz-1) if (e[cur][i]!=fa) {

        LL nup = 1LL;

        if (i>0) nup = nup * fr[i-1] % m;

        if (i<sz-1) nup = nup * bk[i+1] % m;

        dfs2(e[cur][i], cur, nup);

    }

}

int main()

{

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

    REP(i,1,n-1) {

        int x, y;

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

        e[x].push_back(y);

        e[y].push_back(x);

    }



    dfs1(1, -1);

    dfs2(1, -1, 0);

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

    return 0;

}
// Slower C++ solution:
#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;

    }



}


// Faster C++ solution for the same problem (measured speedup: 2.623947641174227x):
#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;

	}



}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 2.632566649425314x):
#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;

}
// 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.4794905983090312x):
#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;

}