#pragma GCC optimize("O2")

#include <bits/stdc++.h>

using namespace std;

typedef long long                   ll;

typedef long double                 ld;

typedef pair<int,int>               pii;

typedef pair<ll,ll>                 pll;

typedef pair<double,double>         pdd;

typedef pair<ld,ld>                 pld;

typedef pair<string , string>       pss;

#define pb(x)                       push_back(x)

#define ts(x)                       to_string(x)

#define ti(x)                       stoi(x)

#define all(x)                      (x).begin(),(x).end()

#define F                           first

#define S                           second

#define Mp                          make_pair

#define tab                         '\t'

#define sep                         ' '

#define mine(v)                     *min_element(all(v))

#define maxe(v)                     *max_element(all(v))

#define error(x)                    cerr << #x << " = " << x << endl

#define fast_io                     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

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

#define endl                        "\n"

inline ll input(){ll n; cin >> n ; return n;}

ll poww(ll a, ll b, ll md) {

    return (!b ? 1 : (b & 1 ? a * poww(a * a % md, b / 2, md) % md : poww(a * a % md, b / 2, md) % md));

}

const int MAXN = 1e6 + 10;

int MOD = 1e9 + 7;

const int MOD2 = 998244353;

const ll INF = 8e18;

ll n;

vector<ll> adj[MAXN];

vector<ll> plm[MAXN];

vector<ll> prm[MAXN];

bool mark[MAXN];

ll dp[MAXN];

ll ans[MAXN];

void dfs1(ll v){

    //cout << ":: " << v << endl;

    mark[v] = true;

    dp[v] = 1;

    ll par = 0;

    for(auto u:adj[v]){

        if(mark[u]){

            par = u;

            continue;

        }

        dfs1(u);

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

        dp[v] %= MOD;

    }

    ll tmp = 1;

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

        ll u = adj[v][i];

        if(u == par){

            plm[v].push_back(tmp);

            continue;

        }

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

        tmp %= MOD;

        plm[v].push_back(tmp);

    }

    tmp = 1;

    for(int i = adj[v].size() - 1;~i;i--){

        ll u = adj[v][i];

        if(u == par){

            prm[v].push_back(tmp);

            continue;

        }

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

        tmp %= MOD;

        prm[v].push_back(tmp);

    }

    reverse(all(prm[v]));

}

void dfs2(ll v,ll cnt){

    mark[v] = true;

    //cout << ":)() < " << v << tab << cnt << endl;

    ans[v] = (dp[v] * cnt) % MOD;

    ll tmp = 1;

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

        ll u = adj[v][i];

        if(mark[u])

            continue;

        if(i > 0){

            tmp = plm[v][i - 1];

        }

        if(i < adj[v].size() - 1){

            tmp *= prm[v][i + 1];

            tmp %= MOD;

        }

        tmp *= cnt;

        tmp++;

        tmp %= MOD;

        dfs2(u,tmp);

    }

}

int main()

{

    cin >> n >> MOD;

    ll a,b;

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

        cin >> a >> b;

        adj[a].push_back(b);

        adj[b].push_back(a);

    }

    dfs1(1);

    memset(mark,0,sizeof(mark));

    dfs2(1 , 1);

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

        cout << i << ": " << endl;

        for (auto j:plm[i]){

            cout << j << sep;

        }

        cout << endl;

        for (auto j:prm[i]){

            cout << j << sep;

        }

        cout << endl;

    }*/

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

        cout << ans[i] << endl;

    }

}



Measured memory_bytes: 95104