//https://www.spoj.com/problems/EC_P/
//https://www.spoj.com/problems/tag/scc
#include<bits/stdc++.h>
#define LL long long
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define I insert
const LL N=100005;
using namespace std;
LL n, m, i, j, k, l, r, t, nn, dp[N], mod, depth[N], d[N], ans;
vector<LL>v[N], pre[N], suf[N];
LL xar(LL a, LL b){
    LL ans=1;
    while(b){
        if(b&1){
            ans=(ans*a)%mod;
        }
        a*=a;a%=mod;
        b>>=1;
    }
    return ans;
}
void go(LL u, LL par=0){
    
    //cout << u << " " << par << endl;
    
    depth[u]=depth[par]+1;
    dp[u]=1;
    for ( LL i = (LL)v[u].size() - 1; i >= 0; i-- ){
        if(v[u][i]==par){
            swap ( v[u][i], v[ u ][ (LL)v[u].size() - 1 ] );
            v[u].pop_back();
            continue;
        }
        go(v[u][i],u);
        dp[u]*=(dp[v[u][i]]+1);
        dp[u]%=mod;
    }
    LL x = 1;
    for ( LL i = 0; i < v[u].size(); ++i ){
        pre[u].PB(x);
        x *= ( dp[v[u][i]] + 1 );
        x %= mod;
    }
    x = 1;
    for ( LL i = (LL)v[u].size() - 1; i >= 0; --i ){
        suf[u].PB(x);
        x *= ( dp[v[u][i]] + 1 );
        x %= mod;
    }
    
    if(suf[u].size() ) reverse ( suf[u].begin (), suf[u].end () );
}
void shota(LL u, LL par=0){
    
    
    //cout << u << " " << par << endl;
    
    for ( LL i = 0; i < v[u].size(); ++i ){
        
        
        LL to = v[u][i];
        //if ( to == par ) continue;
        
        if ( suf[u].size() != v[u].size() ){
            cout<<"wtf\n";
            cout << suf[u].size() << " " << v[u].size() << endl;
            exit(0);
        }
        
        d [to] = d [u];
        
        LL x = pre[u][i] * suf[u][i]; x %= mod;
        
        //cout << to << " " << x << endl;
        
        //x = xar ( x, depth[to] );
        
        d[to] *= x;
        d[to] += 1;
        d[to] %= mod;
        
        //cout<<" a\n";
       // exit(0);
        
        shota ( to, u );
    }
}
void ilia (LL u, LL par = 0 ){
    for ( LL i = 0; i < v[u].size(); ++i ){
        d[v[u][i]] += d[u]; d[v[u][i]] %= mod;
        go (v[u][i], u);
    }
}
int main(){
    
	cin>>n>>mod;
	for ( i = 1; i < n; i++ ){
	    cin >> l >> r; 
	    v[l].PB(r);
	    v[r].PB(l);
	}
	
	d[1]=1;
	
	go(1);
	
	shota(1);
	
	for ( i = 1; i <= n; i++ ){
	    /**/ans = dp[i];// * depth [i]; /*/ ans = xar ( dp[i], depth[i] ); /**/
	    ans *= d [i];
	    ans %= mod;
	    //cout << " " << dp[i] << " " << d[i] << endl;
	    cout << ans << endl;
	}
	return 0;
}
//      IIIIIIIII      OOOOO             A          NN        N    EEEEEEEEEE
//          I         O     O           A A         N N       N    E
//          I        O       O         A   A        N  N      N    E
//          I        O       O        A     A       N   N     N    E
//          I        O       O       AAAAAAAAA      N    N    N    EEEEEEEE
//          I        O       O      A         A     N     N   N    E
//          I        O       O     A           A    N      N  N    E
//          I         O     O     A             A   N       N N    E
//      IIIIIIIII      OOOOO     A               A  N        NN    EEEEEEEEEE ___ KAPANADZE


Measured memory_bytes: 26112