#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long; 
using pii =  pair<int, int>;
using pll =  pair<long long, long long>;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()
constexpr char ln = '\n';
constexpr long long MOD = 1000000007;

/////////////////////////////////////////////////////////////////////////////////

// f は子から親に引き継ぐときに変換するための関数
// merge は子から引き継いだ値をマージするための関数
// h はマージされた結果を用いて最終的な値を求めるための関数
template <typename F, typename Monoid, typename H, typename T>
struct Rerooting {
    int N;
    vector<vector<int>> G;
    vector<T> subdp,dpL,dpR,dp;
    F f;
    Monoid merge;
    H h;
    T unity;

    Rerooting(int N, F f, Monoid merge, H h, T unity) : 
        N(N), G(N), subdp(N), dpL(N), dpR(N), dp(N), 
        f(f), merge(merge), h(h), unity(unity) {}

    void add_edge(int u, int v) {
        G[u].emplace_back(v);
        G[v].emplace_back(u);
    }

    void build(int r = 0) {
        dfs_sub(r,-1);
        dfs_all(r, unity);
    }

private:
    void dfs_sub(int v, int pv) {
        for (int &nv : G[v]) {
            if (nv == pv) swap(nv,G[v].back());
        }
        if (~pv) G[v].pop_back();
        subdp[v] = unity;
        for (int nv : G[v]) {
            dfs_sub(nv,v);
            subdp[v] = merge(subdp[v],f(subdp[nv],nv));
        }
        subdp[v] = h(subdp[v],v);
    }

    //valは親を子に見立てたときのf(subdp[pv],pv)の値
    void dfs_all(int v, T val) {
        dp[v] = val;
        T left = unity;
        for (int i = 0; i < (int)G[v].size(); i++) {
            int nv = G[v][i];
            dpL[nv] = left = merge(left,f(subdp[nv],nv));
            dp[v] = merge(dp[v],f(subdp[nv],nv)); 
        }
        dp[v] = h(dp[v],v);
        T right = unity;
        for (int i = (int)G[v].size()-1; i >= 0; i--) {
            int nv = G[v][i];
            dpR[nv] = right = merge(right,f(subdp[nv],nv));
        }
        for (int i = 0; i < (int)G[v].size(); i++) {
            T L = i == 0 ? unity : dpL[G[v][i-1]];
            T R = i == (int)G[v].size()-1 ? unity : dpR[G[v][i+1]];
            dfs_all(G[v][i],f(h(merge(val,merge(L,R)),v),v));
        }
    }
};

/////////////////////////////////////////////////////////////////////////////////////

template <uint_fast64_t Modulus> 
struct ModInt {
    using u64 = uint_fast64_t;

    u64 a;

    constexpr ModInt(const long long x = 0) noexcept : a(x >= 0 ? x % Modulus : (Modulus - (-x) % Modulus) % Modulus) {}
    constexpr u64 &value() noexcept {return a;}
    constexpr const u64 &value() const noexcept {return a;}
    constexpr ModInt operator+(const ModInt rhs) const noexcept {return ModInt(*this) += rhs;}
    constexpr ModInt operator-(const ModInt rhs) const noexcept {return ModInt(*this) -= rhs;}
    constexpr ModInt operator*(const ModInt rhs) const noexcept {return ModInt(*this) *= rhs;}
    constexpr ModInt operator/(const ModInt rhs) const noexcept {return ModInt(*this) /= rhs;}
    constexpr ModInt operator^(const long long rhs) const noexcept {return ModInt(*this) ^= rhs;}
    constexpr bool operator==(const ModInt &rhs) const noexcept {return a == rhs.a;}
    constexpr bool operator!=(const ModInt &rhs) const noexcept {return a != rhs.a;}
    constexpr ModInt &operator+=(const ModInt rhs) noexcept {
        a += rhs.a;
        if (a >= Modulus) {
            a -= Modulus;
        }
        return *this;
    }
    constexpr ModInt &operator-=(const ModInt rhs) noexcept {
        if (a < rhs.a) {
            a += Modulus;
        }
        a -= rhs.a;
        return *this;
    }
    constexpr ModInt &operator*=(const ModInt rhs) noexcept {
        a = a * rhs.a % Modulus;
        return *this;
    }
    constexpr ModInt &operator/=(ModInt rhs) noexcept {
        u64 exp = Modulus - 2;
        while (exp) {
            if (exp&1) *this *= rhs;
            exp >>= 1;
            rhs *= rhs;
        }
        return *this;
    }
    constexpr ModInt &operator^=(long long exp) noexcept {
        ModInt rhs = a;
        a = 1;
        while (exp) {
            if (exp&1) *this *= rhs;
            exp >>= 1;
            rhs *= rhs;
        }
        return *this;
    }

    friend ostream &operator<<(ostream& os, const ModInt& rhs) noexcept {return os << rhs.a;}
    friend istream &operator>>(istream& is, ModInt& rhs) noexcept {long long a; is >> a; rhs = a; return is;}
};

using mint = ModInt<MOD>;

void EDPC_V() {
    ll N,M; cin >> N >> M;
    auto f=[&](ll a, int v) {(void)v; return (a+1)%M;};
    ll unity = 1;
    auto g=[&](ll a, ll b) {return a*b%M;};
    auto h=[](ll a, int v) {(void)v; return a;};
    Rerooting R(N,f,g,h,unity);
    rep(i,N-1) {
        int u,v; cin >> u >> v;
        --u; --v;
        R.add_edge(u,v);
    }
    R.build();
    rep(i,N) {
        cout << R.dp[i] << ln;
    }
}

struct Node {
    mint dp;
    int sub;
    Node () {}
    Node (mint dp, int sub) : dp(dp), sub(sub) {}
};

void ABC160F() {
    int N; cin >> N;
    vector<mint> kai(N+1,1);
    rep(i,N) kai[i+1] = kai[i]*(i+1);
    auto f=[&](Node a, int v) {(void)v; return Node(a.dp/kai[a.sub],a.sub);};
    Node unity = Node(1,0);
    auto g=[&](Node a, Node b) {return Node(a.dp*b.dp,a.sub+b.sub);};
    auto h=[&](Node a, int v) {(void)v; return Node(a.dp*kai[a.sub],a.sub+1);};
    Rerooting R(N,f,g,h,unity);
    rep(i,N-1) {
        int u,v; cin >> u >> v;
        --u; --v;
        R.add_edge(u,v);
    }
    R.build();
    rep(i,N) {
        cout << R.dp[i].dp << ln;
    }
}

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    EDPC_V();
    //ABC160F();
}

/*
  verified on 2020/08/10
  https://atcoder.jp/contests/dp/tasks/dp_v
  https://atcoder.jp/contests/abc160/tasks/abc160_f
*/

Measured memory_bytes: 18472