#include <bits/stdc++.h>
using namespace std;

struct Benri { Benri() { std::cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(12);}} benri;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vll = vector<long long>;
using vvll = vector<vll>;
using pll = pair<ll, ll>;
using ull = unsigned long long;

template <typename T> using PQ = priority_queue<T>;
template <typename T> using minPQ = priority_queue<T, vector<T>, greater<T>>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define mp make_pair
#define F first
#define S second

template<class T>bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }

//constexpr long long MOD = 1000000007;
//constexpr long long MOD = 998244353;
//constexpr int INF = 1001001001;
constexpr ll INF = 1001001001001001001ll;
constexpr double EPS = 1e-10;
using number = long long;



struct modint {
    ll val;

    static ll &mod() {
        static ll mod_ = 0;
        return mod_;
    }
    static void set_mod(const ll x) { mod() = x; }
    static ll M() {return mod(); }

    modint() : val(0LL) {}
    modint(const ll x) : val(x) {
        val = x % M();
        while (val < 0) val += M();
    }

    modint& operator+=(const modint a) { val += a.val; if (val >= M()) val -= M(); return *this; }
    modint& operator-=(const modint a) { val += a.val; if (val < 0) val += M(); return *this; }
    modint& operator*=(const modint a) { val = val * a.val % M(); return *this; }
    modint& operator/=(const modint a) { val = val * a.inv().val % M(); return *this;}
    modint& operator=(const int& x) {
        val = x % M();
        while (val < 0) val += M();
        return *this;
    }

    modint operator+(const modint a) const { return modint(*this) += a; }
    modint operator-(const modint a) const { return modint(*this) -= a; }
    modint operator*(const modint a) const { return modint(*this) *= a; }
    modint operator/(const modint a) const { return modint(*this) /= a; }
    modint operator-() const { return {M() - val}; }
    modint inv() const {
        ll u = 1, v = 0, s = 0, t = 1, m = M(), x = val;
        while (x) {ll q = M() / x; swap(s -= q * u, u); swap(t -= q * v, v); swap(m -= q * x, x); }
        if (s < 0) s += M();
        return modint(s);
    }
    modint pow(ll n) {
        ll u = 1, xx = val;
        while (n > 0) { if (n & 1) u = u * xx % M(); xx = xx * xx % M(); n >>= 1; }
        return modint(u);
    }
    friend ostream &operator<<(ostream &os, const modint &p) {
        return os << p.val;
    }
    friend istream &operator>>(istream &is, modint &a) {
        ll t;
        is >> t;
        a = modint(t);
        return (is);
    }
};
using mint = modint;


vector<int> E[101010];
//---------------------------------------------------------------------------------------------------
mint dp[101010];
void dfs1(int cu, int pa = -1) {
    dp[cu] = 1;
    for (auto to : E[cu]) if (to != pa) {
            dfs1(to, cu);
            dp[cu] *= dp[to] + 1;
        }
}
//---------------------------------------------------------------------------------------------------
mint ans[101010];
void dfs2(int cu, int pa = -1) {
    ans[cu] = 1;
    for (auto to : E[cu]) ans[cu] *= dp[to] + 1;

    int n = E[cu].size();
    vector<mint> lft(n), rht(n);
    rep(i, n) lft[i] = rht[i] = dp[E[cu][i]] + 1;
    for (int i = 1; i < n; i++) lft[i] *= lft[i - 1];
    for (int i = n - 2; i >= 0; i--) rht[i] *= rht[i + 1];

    //dp[cu]の修正
    rep(i, n) if (E[cu][i] != pa) {
        dp[cu] = 1;
        if (i) dp[cu] *= lft[i - 1];
        if (i + 1 < n) dp[cu] *= rht[i + 1];
        dfs2(E[cu][i], cu);
    }
}
//---------------------------------------------------------------------------------------------------
int main() {
    ll N, M; cin >> N >> M;
    rep(i, N - 1) {
        int x, y; cin >> x >> y;
        x--; y--;
        E[x].push_back(y);
        E[y].push_back(x);
    }

    mint::set_mod(M);
    dfs1(0);
    dfs2(0);

    rep(i, N) cout << ans[i] << endl;
}


Measured memory_bytes: 19584