#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define rrep(i,n) for(int i=((int)(n)-1);i>=0;(i)--)
#define rrep1(i,n) for(int i=((int)(n));i>0;(i)--)
#define sz(c) ((int)(c).size())
#define all(x) (x).begin(),(x).end()
#define mp make_pair
#define pb push_back

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> P;
typedef pair<int, pair<int, int> > PP;
const int MAX=1e5+10;
const ll MOD=1e9+7;
const ll INF=1e18;


int N;
ll M;
vi G[MAX];
vll pr;
vi up;

void input(){
    cin>>N>>M;
    rep(_,N-1){
        int a,b;
        cin>>a>>b;
        a--;b--;
        G[a].pb(b);
        G[b].pb(a);
    }
    ll tmp=M;
    rep1(i,sqrt(M))if(i>1){
        if(tmp%(ll)i==0){
            pr.pb((ll)i);
            int x=0;
            while(tmp%(ll)i==0){
                x++;
                tmp/=i;
            }
            up.pb(x);
        }
    }
    if(tmp>1){
        pr.pb((ll)tmp);
        up.pb(1);
    }
}

ll powm(ll a, int e, ll mod){
    if(a==0)return 0;
    if(e==0)return 1;
    if(e%2==0){
        ll tmp=powm(a, e/2, mod);
        return (tmp*tmp)%mod;
    }
    ll tmp=powm(a, e/2, mod);
    return (((tmp*tmp)%mod)*a)%mod;
}

template<typename T>
T extgcd(T a,T b,T& x, T& y){
	T d=a;
	if(b!=0){
		d = extgcd(b, a%b, y, x);
		y -= (a/b) * x;
	}
	else{
		x = 1; y = 0;
	}
	return d;
}
template<typename T>
T mod_inv(T a,T m){
	T x,y;
	extgcd(a,m,x,y);
	return (m+x%m)%m;
}


vll rev(vll v){
    int l=sz(v);
    vll tmp(l);
    bool zero=false;
    rep(i,l)if(v[i]==0){
        zero=true;
        ll p=1;
        rep(j,l)if(j!=i)p=(p*v[i])%M;
        tmp[i]=p;
        break;
    }
    if(!zero){
        rep(i,l)tmp[i]=1;
        vll x=v;
        rep(k,sz(pr)){
            ll p=pr[k];
            vi ct(l);
            rep(i,l){
                while(x[i]%p==0){
                    ct[i]++;
                    x[i]/=p;
                }
            }
            int sum=0;
            rep(i,l)sum+=ct[i];
            rep(i,l){
                ct[i]=sum-ct[i];
                tmp[i]=(tmp[i]*powm(p, ct[i], M))%M;
            }
        }
        ll prod=1;
        rep(i,l)prod=(prod*x[i])%M;
        rep(i,l){
            tmp[i]=(tmp[i]*prod)%M;
            tmp[i]=(tmp[i]*mod_inv(x[i],(ll)M))%M;
        }
    }
    return tmp;
}

ll dp1[MAX];
ll dfs1(int v, int par){
    ll tmp=1;
    rep(i,sz(G[v]))if(G[v][i]!=par){
        ll k=1+dfs1(G[v][i], v);
        tmp=(tmp*k)%M;
    }
    dp1[v]=tmp;
    return tmp;
}

ll dp2[MAX];
void dfs2(int v, int par, ll info){
    dp2[v]=info+1;
    if(par!=-1){
        vll tmp1;
        rep(i,sz(G[v])){
            int u=G[v][i];
            if(u==par) tmp1.pb(info+1);
            else tmp1.pb(dp1[u]+1);
        }
        vll tmp2=rev(tmp1);
        rep(i,sz(G[v]))if(G[v][i]!=par){
            dfs2(G[v][i], v, tmp2[i]);
        }
    }
    else{
        vll tmp1;
        rep(i,sz(G[v])){
            int u=G[v][i];
            tmp1.pb(dp1[u]+1);
        }
        tmp1.pb(info+1);
        vll tmp2=rev(tmp1);
        rep(i,sz(G[v])){
            dfs2(G[v][i], v, tmp2[i]);
        }
    }
}

ll ans[MAX];
void solve(){
    dfs1(0,-1);
    dfs2(0,-1,0);
    rep(i,N)ans[i]=(dp1[i]*dp2[i])%M;
    rep(i,N)cout<<ans[i]<<endl;
}

int main(){
    input();
    solve();
}

Measured memory_bytes: 27264