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

#ifdef LOCAL 
string to_string(char a) { return string(1,a); }
string to_string(bool a) { return a ? "1" : "0"; }
string to_string(const char* a) { return (string)a; }
string to_string(string a) { return a; }
string to_string(vector<bool> v) {
	string s = "["; for(unsigned i=0;i<v.size();i++){ s += ((i>0)?" ":"");
    s += char('0'+v[i]); } 	s += "]"; return s; }
template<class A, class B> string to_string(pair<A,B> p);
template<class T> string to_string(T v) {
	bool b = 1; string s = "["; for (const auto& x: v) {
		if (!b) {s += ", ";} b = 0; s += to_string(x); } s += "]"; return s; }
template<class A, class B> string to_string(pair<A,B> p) {
	return "("+to_string(p.first)+", "+to_string(p.second)+")"; }
void dbgf() { cerr << endl; }
template<class H, class... T> void dbgf(H h, T... t) {
	cerr << to_string(h); if (sizeof...(t)) cerr << ", ";
	dbgf(t...); }
#define dbg(...) cerr << #__VA_ARGS__ << " = ", dbgf(__VA_ARGS__)
#else
#define dbg(...) 0
#endif
#define boolout(x) cout<<(x?"YES":"NO")<<endl;
typedef long long ll;

int mod;

int add(int a, int b){
    a+=b;
    if (a>=mod) {
        a-=mod;
    }
    return a;
}
int mul(int a, int b){
    return (ll) a * b % mod;
}
const int nmax = 1e5+5;
//const int nmax = 4;
vector<int> g[nmax];
vector<int> children[nmax];
vector<pair<int, int>> down;
vector<pair<int, int>> downs[nmax], prefs[nmax], sufs[nmax];
vector<pair<int, int>> up;
#define all_white first
#define sth_black second
pair<int, int> merge(pair<int, int>a, pair<int, int> b){
    pair<int, int> p = {1, 0};
    p.all_white = mul(a.all_white, b.all_white);
    p.sth_black = add(mul(a.sth_black, add(b.all_white, b.sth_black)),
        mul(a.all_white, b.sth_black));
    return p;
}
pair<int, int> add_black(pair<int, int> p){
    return {p.all_white, add(p.all_white, p.sth_black)};
}

void dfs1(int v, int par){
    pair<int, int> p = {1, 0};
    for (int to : g[v]) {
        if (to!=par) {
            children[v].push_back(to);
            dfs1(to, v);
            downs[v].push_back(down[to]);
            p = merge(p, down[to]);
        }
    }
    down[v] = add_black(p);
}

void dfs2(int v, int par){
    for (int i = 0; i < (int) children[v].size(); ++i) {
       pair<int, int> p = {1, 0};
       if (par!=-1) {
           p = merge(p, up[v]);
       }
       if (i!=0) {
           p = merge(p, prefs[v][i-1]);
       }
       if (i!=(int)children[v].size()-1) {
          p = merge(p, sufs[v][i+1]); 
       }
       up[children[v][i]] = add_black(p);
       dfs2(children[v][i], v);
    }
}


void solve()
{
    int n;
    cin >> n  >> mod;
    down.resize(nmax);
    up.resize(nmax);
    for (int i = 0; i < n-1; ++i) {
        int t1, t2;
        cin >> t1 >> t2;
        g[t1].push_back(t2);
        g[t2].push_back(t1);
    }
    dfs1(1, -1);
    dbg(down);
    for (int i = 1; i < n+1; ++i) {
        int k = downs[i].size();
        pair<int, int> p = {1, 0};
        for (int j = 0; j < k; ++j) {
            p = merge(p, downs[i][j]);
            prefs[i].push_back(p);
        }
        p = {1, 0};
        for (int j = k-1; j >= 0; --j) {
            p = merge(p, downs[i][j]);
            sufs[i].push_back(p);
        }
        reverse(sufs[i].begin(), sufs[i].end());
    }
    dfs2(1, -1);
    for (int i = 1; i < n+1; ++i) {
        pair<int, int> p = {1, 0};
        if (i!=1) {
            p = merge(p, up[i]);
        }
        if (children[i].size()) {
            p = merge(p, prefs[i].back());
        }
        p = add_black(p);
        cout << p.sth_black << endl;
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t=1;
    //cin >> t; // read t. cin knows that t is an int, so it reads it as such.
    for (int i = 1; i <= t; ++i) {
        solve();
    }
    return 0;
}



Measured memory_bytes: 39856