#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
using namespace std;
typedef long long ll;

class Tree{
public:
    int N;
    ll MOD;
    vector<vector<int>> G;
    vector<vector<int>> Gd;
    vector<ll> dp; //iの部分木の条件を満たす塗り方(iは黒)
    vector<ll> ans;

    Tree(int n, int mod){
        N = n;
        MOD = mod;
        G.resize(N, vector<int>(0));
        Gd.resize(N, vector<int>(0));
        dp.resize(N, 0);
        ans.resize(N, 0);
    }

    void makeEdge(int s, int t){
        G[s].push_back(t);
    }

    ll dfs(int v, int pa){
        ll res = 1;
        for(int i = 0; i < (int)G[v].size(); i++){
            if(G[v][i] == pa) continue;
            Gd[v].push_back(G[v][i]);
            res *= dfs(G[v][i], v) + 1;
            res %= MOD;
        }
        dp[v] = res;
        return res;
    }

    /*
    void dfs2(int v, int pa, ll d_pa){
        ans[v] = d_pa;
        int sz = G[v].size();
        for(int i = 0; i < sz; i++){
            if(G[v][i] != pa){
                ans[v] *= dp[G[v][i]] + 1;
                ans[v] %= MOD;
            }
        }

        for(int i = 0; i < sz; i++){
            if(G[v][i] != pa){
                ll sm = d_pa;
                for(int j = 0; j < sz; j++){
                    if(i != j && G[v][j] != pa){
                        sm *= (dp[G[v][j]] + 1);
                        sm %= MOD;
                    }
                }

                dfs2(G[v][i], v, sm);
            }
        }
    }
    */


    void dfs2(int v, int pa){
        ans[v] = 1;
        int sz = G[v].size();
        for(int i = 0; i < sz; i++){
            ans[v] *= dp[G[v][i]] + 1;
            ans[v] %= MOD;
        }

        ll lft[sz], rht[sz];
        for(int i = 0; i < sz; i++){
            lft[i] = dp[G[v][i]] + 1;
            rht[i] = dp[G[v][i]] + 1;
        }
        for(int i = 1; i < sz; i++){
            lft[i] *= lft[i - 1];
            lft[i] %= MOD;
        }
        for(int i = sz - 2; i >= 0; i--){
            rht[i] *= rht[i + 1];
            rht[i] %= MOD;
        }

        for(int i = 0; i < sz; i++){
            dp[v] = 1;
            if(G[v][i] != pa){
                if(i > 0) dp[v] *= lft[i - 1];
                if(i + 1 < sz) dp[v] *= rht[i + 1];
                dp[v] %= MOD;
                dfs2(G[v][i], v);
            }
        }
    }
};

int main(){
    int N, mod;
    cin >> N >> mod;

    Tree tr = Tree(N, mod);

    for(int i = 0; i < N - 1; i++){
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        tr.makeEdge(a, b);
        tr.makeEdge(b, a);
    }

    tr.dfs(0, -1);
    tr.dfs2(0, -1);

    for(int i = 0; i < N; i++){
        cout << tr.ans[i] << endl;
    }

    /*
    cout << endl;
    for(int i = 0; i < N; i++){
        cout << i << " " << tr.dp[i] << endl;
    }

    //debug

    cout << endl;
    for(int i = 0; i < N; i++){
        cout << i << endl;
        for(int j = 0; j < (int)tr.Gd[i].size(); j++){
            cout << tr.Gd[i][j] << " ";
        }
        cout << endl;
    }
    */

    return 0;
}


Measured memory_bytes: 23424