#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;

//typedef
typedef long long LL;
typedef vector<LL> VL;
typedef vector<VL> VVL;
typedef vector<string> VS;
typedef pair<LL, LL> PLL;

//conversion
inline LL toLong(string s) {LL v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) LL((a).size())
#define EACH(i,c) for(auto i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
#define FOR(i,a,b) for(LL i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define UNTIL(p) while(!(p))

//constant
const double EPS = 1e-5;
const double PI  = acos(-1.0);

//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

#define PUTS(x) cout << (x) << endl;

template<class T>
class Segtree{
	unsigned int n;
	unsigned int size;
	vector<T> node;
	T val;
	function< T(T,T)> f;
public:
	Segtree(unsigned int a_n, T a_val, function< T(T,T)> a_f) :n(1),val(a_val),f(a_f){
		while(n<a_n)n<<=1;
		size = n*2-1;
		node.assign(size,val);
	}
	void set(int i, T x){
		node[i+n-1]=x;
	}
	T get(int i){
		return node[i+n-1];
	}
	void update(){
		for(int i=n-2;i>=0;i--){
			node[i] = f(node[i*2+1],node[i*2+2]);
		}
	}
	void update(int i, T x){
		i += n-1;
		node[i]=x;
		while(i>0){
			i = (i-1)/2;
			node[i] = f(node[i*2+1],node[i*2+2]);
		}
	}
	T query(int a,int b){
		return _query(a,b,0,0,n);
	}
	T _query(int a,int b,int i,int l,int r){
		if(r<=a || b<=l) return val;
		if(a<=l && r<=b) return node[i];
		else{
			T vl = _query(a,b,i*2+1,l,(l+r)/2);
			T vr = _query(a,b,i*2+2,(l+r)/2,r);
			return f(vl,vr);
		}
	}
};


LL n,m;

VL links[100005];
LL dp1[100005] = {0};
LL dp2[100005] = {0};
map<LL,LL> dp1_sub[100005];
map<LL,LL> dp2_sub[100005];

void erase_parent(LL i, LL parent){
    if(parent != -1){
        links[i].erase(find(ALL(links[i]),parent));
    }
    for(LL child : links[i]){
        erase_parent(child, i);
    }
}

LL f(LL i){
    dp1[i] = 1;
    for(LL child : links[i]){
        dp1[i] *= (f(child) + 1) % m;
        dp1[i] %= m;
    }
	Segtree<LL> s(links[i].size(),1ll,
	    [](LL a,LL b){return (a*b)%m;});
    REP(j,links[i].size()){
        s.set(j, dp1[links[i][j]] + 1);
    }
    s.update();
    REP(j, links[i].size()){
        LL left = s.query(0,j);
        LL right = s.query(j+1,links[i].size());
        dp1_sub[i][links[i][j]] = (left * right) % m;
    }
    //for(LL j : links[i]){
    //    LL sub = 1;
    //    for(LL child : links[i]){
    //        if(child == j) continue;
    //        sub *= (dp1[child] + 1) % m;
    //        sub %= m;
    //    }
    //    dp1_sub[i][j] = sub;
    //}
    return dp1[i];
}

LL g(LL i, LL parent){
    dp2[i] = dp1[i];
    if(parent != -1){
        dp2[i] += dp1[i] * dp2_sub[parent][i];
        dp2[i] %= m;
    }
    for(LL j : links[i]){
        LL sub = dp1_sub[i][j];
        if(parent != -1){
            sub += dp1_sub[i][j] * dp2_sub[parent][i];
        }
        dp2_sub[i][j] = sub % m;
    }
    for(LL child : links[i]){
        g(child, i);
    }
    return dp2[i];
}

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

    cin>>n>>m;
    REP(i,n-1){
        LL x,y;
        cin>>x>>y;
        x--;
        y--;
        links[x].PB(y);
        links[y].PB(x);
    }
    erase_parent(0, -1);

    f(0);
    g(0, -1);

    REP(i,n){
        PUTS(dp2[i]);
    }

}

Measured memory_bytes: 108416