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

int64_t mod;
class modint{
    uint64_t value;
public:
    modint() noexcept: value(0){ }
    modint(const int64_t x) noexcept: value(x % mod + (x < 0 ? mod : 0)){ }
    explicit operator uint64_t() const noexcept{ return value; }
    modint inverse() const noexcept{ return mpow(*this, mod-2); }
    bool operator==(const modint &rhs) const noexcept{ return value == rhs.value; }
    bool operator!=(const modint &rhs) const noexcept{ return value != rhs.value; }
    modint operator+() const noexcept{ return modint(*this); }
    modint operator-() const noexcept{ return modint(mod - value); }
    modint operator+(const modint &rhs) const noexcept{ return modint(*this) += rhs; }
    modint operator-(const modint &rhs) const noexcept{ return modint(*this) -= rhs; }
    modint operator*(const modint &rhs) const noexcept{ return modint(*this) *= rhs; }
    modint operator/(const modint &rhs) const noexcept{ return modint(*this) /= rhs; }
    modint &operator+=(const modint &rhs) noexcept{
        if((value += rhs.value) >= mod) value -= mod;
        return *this;
    }
    modint &operator-=(const modint &rhs) noexcept{ return *this += mod - rhs.value; }
    modint &operator*=(const modint &rhs) noexcept{
        if((value *= rhs.value) >= mod) value %= mod;
        return *this;
    }
    modint &operator/=(const modint &rhs) noexcept{ return *this *= rhs.inverse(); }
    modint operator++(int) noexcept{
        modint ret(*this);
        if((++value) >= mod) value -= mod;
        return ret;
    }
    modint operator--(int) noexcept{
        modint ret(*this);
        if((value += mod - 1) >= mod) value -= mod;
        return ret;
    }
    modint &operator++() noexcept{ return *this += 1; }
    modint &operator--() noexcept{ return *this -= 1; }
    friend std::ostream &operator<<(std::ostream &os, const modint &x){ return os << x.value; }
    friend std::istream &operator>>(std::istream &is, modint &x){
        int64_t i;
        is >> i;
        x = modint(i);
        return is;
    }
    friend modint mpow(const modint &x, uint64_t y){
        modint ret{1}, m{x};
        while(y > 0){
            if(y & 1) ret *= m;
            m *= m;
            y >>= 1;
        }
        return ret;
    }
};

int n;
vector<int> edge[100000];
int parent[100000], idx[100000];
vector<modint> lmul[100000], rmul[100000];
modint pp[100000];

modint solve(int v, int i=-1, int p=-1){
    if(p >= 0){
        parent[v] = p;
        idx[v] = i;
    }
    vector<modint> ch;
    lmul[v].push_back(1);
    rmul[v].push_back(1);
    for(size_t i=0;i<edge[v].size();++i){
        if(edge[v][i] == p) ch.emplace_back(1);
        else ch.emplace_back(solve(edge[v][i], i, v));
    }
    for(size_t i=0;i<edge[v].size();++i){
        lmul[v].emplace_back(lmul[v].back() * ch[i]);
        rmul[v].emplace_back(rmul[v].back() * ch[edge[v].size()-1-i]);
    }
    return lmul[v].back() + 1;
}

void rsolve(int v, int p=-1){
    if(p >= 0) pp[v] = lmul[p][idx[v]] * rmul[p][edge[p].size()-idx[v]-1] * pp[p] + 1;
    for(auto c: edge[v]) if(c != p) rsolve(c, v);
}

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

    cin >> n >> mod;
    for(int i=1;i<n;++i){
        int x, y;
        cin >> x >> y;
        --x; --y;
        edge[x].emplace_back(y);
        edge[y].emplace_back(x);
    }
    for(int i=0;i<n;++i) pp[i] = 1;
    solve(0);
    rsolve(0);    
    for(int i=0;i<n;++i) cout << lmul[i].back() * pp[i] << endl;

    return 0;
}

Measured memory_bytes: 31232