#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <stack>
#include <tuple>
#include <cmath>
#include <iomanip>
#include <map>
#include <cstring> //memset(dp,0,sizeof(dp))
#include <functional>
#include <cctype>
#include <locale>
#define ll long long
#define rep(i,n) for(int i=0;i<(n);i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define ALL(a) (a).begin(),(a).end()
using namespace std;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
typedef pair<int,int> P;
typedef pair<long long,long long> Pll;
#define fout(num) cout << fixed << setprecision(20) << (num) << endl
//s[i]=tolower(s[i]); islower(s[i]); cout << tolower(s[i])はバグ
//vector<vector<ll>> dp(n,vector<ll>(n))
//exist x map o setconst ll MOD = 1e9+7;return fac[n]*(finv[k]*finv[n-k] % MOD) % MOD;
//2-dim:vector<vector<Type>> vv(n, vector<Type>(m, d));
//3-dim:vector<vector<vector<Type>>> vvv(n, vector<vector<Type>>(m, vector<Type>(l, d)));

int MOD;

struct Rerooting{
    /*  begin here  */
    struct DP{
        long long dp;
        DP(long long dp_):dp(dp_){}
    };
    const DP identity=DP(1);
    function<DP(DP,DP)> merge = [](DP dp_cum,DP d) -> DP{ return DP((dp_cum.dp*d.dp)%MOD); };
    function<DP(DP)> add_root = [](DP d) -> DP{ return DP(d.dp+1); };
    /*  end here   */
    struct Edge{
        int to;
    };
    using Graph=vector<vector<Edge>>;
    vector<vector<DP>> dp; //dp[i][j]:=from i number j path
    vector<DP> ans;
    Graph G;
    Rerooting(int N):G(N){
        dp.resize(N);
        ans.assign(N, identity);
    }
    void add_edge(int a,int b){
        G[a].push_back({b});
    }
    void build(){
        dfs(0);
        bfs(0,identity);
    }
    DP dfs(int v_,int par_=-1){
        DP dp_cum_=identity;
        int deg_=(int)G[v_].size();
        dp[v_]=vector<DP>(deg_,identity);
        for(int i_=0;i_<deg_;i_++){
            int u_=G[v_][i_].to;
            if(u_==par_) continue;
            dp[v_][i_]=dfs(u_,v_);
            dp_cum_=merge(dp_cum_,dp[v_][i_]);
        }
        return add_root(dp_cum_);
    }
    void bfs(int v_,const DP &dp_par,int par_=-1){
        int deg_=(int)G[v_].size();
        for(int i_=0;i_<deg_;i_++){
            if(G[v_][i_].to==par_) dp[v_][i_]=dp_par;
        }
        vector<DP> dp_l(deg_+1,identity),dp_r(deg_+1,identity);
        for(int i_=0;i_<deg_;i_++){
            dp_l[i_+1] = merge(dp_l[i_],dp[v_][i_]);
        }
        for(int i_=deg_-1;i_>=0;i_--){
            dp_r[i_] = merge(dp_r[i_+1],dp[v_][i_]);
        }
        ans[v_]=add_root(dp_l[deg_]);
        for(int i_=0;i_<deg_;i_++){
            int u_=G[v_][i_].to;
            if(u_==par_) continue;
            bfs(u_,add_root(merge(dp_l[i_], dp_r[i_+1])),v_);
        }
    }
};

signed main(){
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    int n,m; cin >> n >> m;
    MOD=m;
    Rerooting tree(n);
    rep(i,n-1){
        int x,y; cin >> x >> y;
        x--; y--;
        tree.add_edge(x, y);
        tree.add_edge(y, x);
    }
    tree.build();
    rep(i,n) cout << tree.ans[i].dp-1 << endl;
}




Measured memory_bytes: 34292