// #pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize ("-ffloat-store")
#include "bits/stdc++.h"
using namespace std;
#define ll long long int
// #define ll int
#define double /*long*/ double
#define endl '\n'
#define vll vector<ll>
#define vvl vector<vll>
#define pll pair<ll,ll>
#define vpll vector<pll>
#define vppll vector<pair<pll,pll>>
#define mp make_pair
#define pb push_back
#define mapll map<ll,ll>
#define fir first
#define sec second
#define _cin ios_base::sync_with_stdio(0);  cin.tie(0);
#define fo(i,b) for(i=0;i<b;++i)
#define repa(i,a,b) for(i=a;i<b;++i)
#define repb(i,a,b) for(i=a;i>=b;--i)
#define all(x) (x).begin(), (x).end()
#define s(v) (ll)v.size()
#define watch(x) cerr << (#x) << " is " << (x) << endl
#define watch2(x,y) cerr << (#x) << " is " << (x) << " and " << (#y) << " is " << (y) << endl
#define Time cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n"
#define inf 2000000000000000000 
#define mod  1000000007
#define mod1 998244353
const int SZ = 1e5+10;
 
// #include <ext/pb_ds/assoc_container.hpp> 
// #include <ext/pb_ds/tree_policy.hpp> // order_of_key() ; *( find_by_order() ) -> kth element in set;
// using namespace __gnu_pbds;  // order_of_key(k) -> Number of elements strictly smaller than k
// #define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>

inline ll max(ll a,ll b,ll c) {return max(max(a, b), c);}
inline ll min( ll a, ll b, ll c) { return min(min(a, b), c);}
inline ll max(ll a,ll b) {return (a>b) ? a : b;}
inline ll min(ll a,ll b) {return (a<b) ? a : b;}
inline ll mul(ll x, ll y, ll mod_){return ((x%mod_)*1LL*(y%mod_))%mod_;}
ll power(ll a, ll n) {ll p = 1;while (n > 0) {if(n%2) {p = p * a;} n >>= 1; a *= a;} return p;}
ll powm(ll a, ll n, ll mod_){ ll p=1; while(n){ if(n%2){ p=mul(p,a,mod_);} n>>=1; a=mul(a,a,mod_);} return p%mod_;} 
ll powi(ll a, ll mod_) {return powm(a,mod_-2,mod_);}
                          /*Code Begins*/
vvl graph;
vll par, dp;
int md;
void dfs(ll x){
    for(auto y : graph[x]){
        if(y!=par[x]){
            par[y]=x;
            dfs(y);
            dp[x] = mul(dp[x],dp[y]+1,md);
        }
    }
}
vll ans;
void dfs2(ll x, ll c){
    ll sz=s(graph[x]);
    ans[x] = mul(dp[x],c,md);
    vll pr(sz+1), su(sz+1);
    pr[0]=su[sz]=1;
    ll i;
    repa(i,1,sz+1){
        if(graph[x][i-1]!=par[x]) pr[i] = mul(pr[i-1],dp[graph[x][i-1]]+1,md);
        else pr[i] = mul(pr[i-1],c,md);
    }
    repb(i,sz-1,0){
        if(graph[x][i]!=par[x]) su[i] = mul(su[i+1],dp[graph[x][i]]+1,md);
        else su[i] = mul(su[i+1],c,md);
    }
    // cout << x << ' ' << endl;
    // for(auto x : pr) cout << x << ' ';
    //     cout << endl;
    // for(auto x : su) cout << x << ' ';
    //     cout << endl;
    fo(i,sz){
        if(graph[x][i]!=par[x]){
            dfs2(graph[x][i], (mul(pr[i],su[i+1],md)+1)%md);
        }
    }
}




int main(){
    #ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
    #endif
    _cin;
    // cout << setprecision(15);
    ll mn, mx;
    ll n, m, t, k, i, j, sum=0, flag=0, cnt=0;
    ll x=0, y=0, z, l , r, q;
    int TC=1;
    // cin >> TC;
while(TC--){
    cin >> n >> md;
    graph.resize(n);
    par.assign(n,-1);
    ans.resize(n);
    dp.assign(n,1);
    fo(i,n-1){
        cin >> x >> y;
        --x;--y;
        graph[x].pb(y);
        graph[y].pb(x);
    }
    dfs(0);
    dfs2(0,1);
    for(auto x : ans) cout << x <<  endl;

}   

    Time;
    return 0;
}

Measured memory_bytes: 20608