#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize ("Ofast")
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
 
using namespace __gnu_pbds;
using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key
 
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
 
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<long long,long long> ii; 	
typedef complex<long double> com;

const ll mod = 1e9+7;

ll m;
vector <vector<ll>> graph;
vector <ll> dp;
vector <vector<ll>> pre;
vector <vector<ll>> suf;
vector <ll> ans;

void dfs(ll u = 1,ll p = 0){
    dp[u] = 1;
    for (ll v :graph[u]){
        if (v == p) continue;
        dfs(v,u);
        dp[u] *= (dp[v]+1); 
        dp[u] %= m;
    }
}

void dfs1(ll u,ll p,ll v){
    ans[u] = (dp[u]*(v+1))%m;
    pre[u].resize(graph[u].size()+1,1);
    suf[u].resize(graph[u].size()+1,1);
    for (int z=0;z<graph[u].size();z++){
        if (graph[u][z] == p){
            if (z != 0) pre[u][z] = pre[u][z-1];
            continue;
        }
        else if (z == 0) pre[u][z] = dp[graph[u][z]]+1;
        else pre[u][z] = pre[u][z-1]*(dp[graph[u][z]]+1);
        pre[u][z] %= m;
    }
    for (int z=graph[u].size()-1;z>=0;z--){
        if (graph[u][z] == p){
            if (z != graph[u].size()-1) suf[u][z] = suf[u][z+1];
            continue;
        }
        if (z == graph[u].size()-1) suf[u][z] = dp[graph[u][z]]+1;
        else suf[u][z] = suf[u][z+1]*(dp[graph[u][z]]+1);
        suf[u][z] %= m;
    }
    for (int z=0;z<graph[u].size();z++){
        if (graph[u][z] == p) continue;
        ll value = 1;
        if (z > 0) value *= pre[u][z-1];
        value %= m;
        if (z < graph[u].size()-1) value *= suf[u][z+1];
        value %= m;
        dfs1(graph[u][z],u,(value*(v+1))%m);
    }
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
    ll n;
    cin >> n >> m;
    graph.resize(n+1);
    for (int z=0;z<n-1;z++){
        ll u,v;
        cin >> u >> v;
        graph[u].push_back(v);
        graph[v].push_back(u);
    }
    dp.resize(n+1);
    ans.resize(n+1);
    pre.resize(n+1);
    suf.resize(n+1);
    dfs();
    ans[1] = dp[1];
    pre[1].resize(graph[1].size()+1,1);
    suf[1].resize(graph[1].size()+1,1);
    for (int z=0;z<graph[1].size();z++){
        if (z == 0) pre[1][z] = dp[graph[1][z]]+1;
        else pre[1][z] = pre[1][z-1]*(dp[graph[1][z]]+1);
        pre[1][z] %= m;
    }
    for (int z=graph[1].size()-1;z>=0;z--){
        if (z == graph[1].size()-1) suf[1][z] = dp[graph[1][z]]+1;
        else suf[1][z] = suf[1][z+1]*(dp[graph[1][z]]+1);
        suf[1][z] %= m;
    }
    for (int z=0;z<graph[1].size();z++){
        ll value = 1;
        if (z > 0) value *= pre[1][z-1];
        value %= m;
        if (z < graph[1].size()-1) value *= suf[1][z+1];
        value %= m;
        dfs1(graph[1][z],1,value);
    }
    for (int z=1;z<=n;z++){
        cout << (ans[z])%m << endl;
    }
}


Measured memory_bytes: 28416