#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <complex>
#include <cstring>
using namespace std;

using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1001001001;
// constexpr int mod = 1000000007;
constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, M;
    cin >> N >> M;
    vector<vector<int>> graph(N);
    for(int i = 0; i < N - 1; ++i){
        int x, y;
        cin >> x >> y;
        --x, --y;
        graph[x].emplace_back(y);
        graph[y].emplace_back(x);
    }

    vector<ll> dp(N);
    auto dfs = [&](auto&& self, int from = 0, int par = -1) -> ll {
        ll res = 1;
        for(int to : graph[from]){
            if(to == par)   continue;
            res *= self(self, to, from) + 1;
            res %= M;
        }
        return dp[from] = res;
    };
    dfs(dfs);

    vector<ll> dp2 = dp;
    vector<vector<ll>> prefix(N), suffix(N);
    auto rerooting = [&](auto&& self, int root = 0, int par = -1) -> void {
        int n = graph[root].size();
        if(root == 0){
            prefix[root].emplace_back(1);
            suffix[root].emplace_back(1);
            for(int v : graph[root]){
                prefix[root].emplace_back(dp[v] + 1);
                suffix[root].emplace_back(dp[v] + 1);
            }
            suffix[root].emplace_back(1);
            for(int i = 0; i < n; ++i){
                prefix[root][i + 1] *= prefix[root][i];
                prefix[root][i + 1] %= M;
            }
            for(int i = n - 1; i >= 0; --i){
                suffix[root][i] *= suffix[root][i + 1];
                suffix[root][i] %= M;
            }
            for(int i = 0; i < n; ++i){
                int nxt_root = graph[root][i];
                ll val = (prefix[root][i] * suffix[root][i + 2] + 1) % M;
                dp2[nxt_root] *= val;
                dp2[nxt_root] %= M;
                prefix[nxt_root].emplace_back(val);
                suffix[nxt_root].emplace_back(val);
                self(self, nxt_root, root);
            }
        }
        else{
            for(int v : graph[root]){
                if(v == par)    continue;
                prefix[root].emplace_back(dp[v] + 1);
                suffix[root].emplace_back(dp[v] + 1);
            }
            suffix[root].emplace_back(1);
            for(int i = 0; i < n; ++i){
                prefix[root][i + 1] *= prefix[root][i];
                prefix[root][i + 1] %= M;
            }
            for(int i = n - 1; i >= 0; --i){
                suffix[root][i] *= suffix[root][i + 1];
                suffix[root][i] %= M;
            }
            int idx = 0;
            for(int nxt_root : graph[root]){
                if(nxt_root == par) continue;
                ll val = (prefix[root][idx] * suffix[root][idx + 2] + 1) % M;
                dp2[nxt_root] *= val;
                dp2[nxt_root] %= M;
                prefix[nxt_root].emplace_back(val);
                suffix[nxt_root].emplace_back(val);
                ++idx;
                self(self, nxt_root, root);
            }
        }
    };
    rerooting(rerooting);

    for(int i = 0; i < N; ++i)  cout << dp2[i] << '\n';
}

Measured memory_bytes: 31312