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

#define rep(i, n) for(int (i) = 0; (i) < (n); (i)++)
#define repp(i, n, m) for(int (i) = (n); (i) < (m); (i)++)
#define repn(i, n) for(int (i) = 1; (i) <= (n); (i)++)
#define repr(i, n) for(int (i) = (n-1); (i) >= 0; (i)--)
#define all(x) (x).begin(), (x).end()
#define lint long long
#define ulint unsigned long long
#define ldou long double
#define fi first
#define se second
#define setpre(x) std::cout << fixed << setprecision(x)
#define out(x) cout << (x) << endl
#define outs(x) cout << (x) << " "
#define bit_c(x) __builtin_popcountll(x)

inline void logger(){ std::cout << " [LGR] " << endl; }
template<typename A, typename... B>
void logger(const A& a, const B&... b){
    cout << a << " , ";
    logger(b...);
}

typedef pair<lint, lint> P;
const lint MOD = 1000000007;
const lint MOD9 = 998244353;
const lint INF = MOD * MOD;
const int MAX = 100005;

/* ...o(^-^)o... */

class amint{
    public:
        long long x;
        static long long mod;
        static void set_mod(long long m) { mod = m; }

    amint(long long _x = 0) : x((_x%mod+mod)%mod) {}

    amint operator-() const{
        return amint(-x);
    }
    amint& operator+=(const amint& a){
        if((x += a.x) >= mod) x -= mod;
        return *this;
    }
    amint& operator-=(const amint& a){
        if((x += mod-a.x) >= mod) x -= mod;
        return *this;
    }
    amint& operator*=(const amint& a){
        (x *= a.x) %= mod;
        return *this;
    }
    amint operator+(const amint& a) const{
        amint res(*this);
        return res+=a;
    }
    amint operator-(const amint& a) const{
        amint res(*this);
        return res-=a;
    }
    amint operator*(const amint& a) const{
        amint res(*this);
        return res*=a;
    }
    amint pow(long long x) const{
        if(!x) return 1;
        amint a = pow(x>>1);
        a *= a;
        if(x&1) a *= *this;
        return a; 
    }
    amint inv() const{
        long long a = x, b = mod, u = 1, v = 0, t;
        while(b > 0){
            t = a / b;
            swap(a -= t * b, b);
            swap(u -= t * v, v);
        }
        return amint(u);
    }
    amint& operator/=(const amint& a){
        return (*this) *= a.inv();
    }
    amint operator/(const amint& a) const{
        amint res(*this);
        return res/=a;
    }
    friend istream& operator>>(istream& is, amint& m){
        long long x; is >> x; m = x;
        return is;
    }
    friend ostream& operator<<(ostream& os, const amint& m){
        os << m.x; return os;
    }
};
long long amint::mod = 1;

lint n, m;
vector<int> to[MAX];
amint dp[MAX], rev[MAX], ans[MAX];
vector<amint> cum1[MAX], cum2[MAX];
int par[MAX], id[MAX];

void dfs(int s, int p = -1){
    dp[s] = 1; par[s] = p;
    int i = 0;
    for(int t : to[s]){
        if(t == p) continue;
        dfs(t, s);
        dp[s] *= dp[t]+1;
    }
}

void cum(){
    rep(i, n){
        cum1[i].push_back(1);
        cum2[i].push_back(1);

        rep(j, to[i].size()){
            if(to[i][j] == par[i]) continue;
            cum1[i].push_back(cum1[i].back()*(dp[to[i][j]]+1));
            id[to[i][j]] = cum1[i].size()-1;
        }
        repr(j, to[i].size()){
            if(to[i][j] == par[i]) continue;
            cum2[i].push_back(cum2[i].back()*(dp[to[i][j]]+1));
        }
        reverse(all(cum2[i]));
    }
}

int main(){
    cin >> n >> m;
    amint::set_mod(m);

    rep(i, n-1){
        int x, y; cin >> x >> y; x--; y--;
        to[x].push_back(y); to[y].push_back(x);
    }
    dfs(0); cum();

    queue<int> que; que.push(0);
    while(!que.empty()){
        int s = que.front(); que.pop();
        for(int t : to[s]) if(t != par[s]) que.push(t);

        if(par[s]<0) { rev[s]=0; continue; }
        rev[s] = (rev[par[s]]+1)*cum1[par[s]][id[s]-1]*cum2[par[s]][id[s]];
    }

    rep(i, n){
        ans[i] = rev[i]+1;
        for(int j : to[i]) if(j != par[i]) ans[i] *= dp[j]+1;
        out(ans[i]);
    }
}


Measured memory_bytes: 25472