#include <iostream>
#include <algorithm>
#include <utility>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>
#include <math.h>
#include <string.h>
#include <iomanip>
#include <numeric>
#include <cstdlib>
#include <cstdint>
#include <cmath>
#include <functional>
#include <limits>

using namespace std;

using ll = long long;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vll = vector<vl>;
using vpll = vector<pll>;

void debug_out() { cout << endl; }
 
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cout << H << " ";
  debug_out(T...);
}

#ifdef LOCAL
#define debug(...) cout << "debug: "; debug_out(__VA_ARGS__)
#else
#define debug(...) 
#endif

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define Rep(i, n) for (long long i = 0; i< (long long)(n); i++)
#define RRep(i,n) for (long long i = ((long long)(n)-1); i>=0; i--)
#define all(v) (v).begin(),(v).end()

struct Edge{
    int to;
    int weight;
    Edge(int t, int w) : to(t), weight(w){ }
};

struct edge{
    int from;
    int to;
    int weight;
    edge(int f,int t,int w) : from(f), to(t), weight(w){ }
};


using Graph = vector<vector<Edge>>;
using graph = vector<vector<int>>;
using edges = vector<edge>;

template <class T>
inline bool chmin(T &a, T b){
    if (a > b){
        a = b;
        return true;
    }
    return false;
}

template <class T>
inline bool chmax(T &a, T b){
    if (a < b){
        a = b;
        return true;
    }
    return false;
}

template <class T>
T gcd(T a, T b){
    if( b==0 ) return a;
    return gcd(b, a%b);
}

template <class T>
T lcm(T a, T b){
    return (a*b)/gcd(a,b);
}

bool isPalindrome(string s){
    string t = s;
    reverse(all(t));
    return s==t;
}

constexpr int INF = 1e9+7;

vector<int> dx = {-1,0,1,0};
vector<int> dy = {0,-1,0,1};

int n,m;
graph g;
vector<vector<ll>> dp;

ll dfs(const int p,const int v){
    ll res = 1;
    rep(i,g[v].size()){
        if(g[v][i] != p){
            dp[v][i] = dfs(v,g[v][i]);
            res *= (dp[v][i]+1);
            res %=m;
        }
    }
    return res;
}

void Rev(const int p,const int v,const ll add){
    rep(i,g[v].size()){
        if(g[v][i] == p){
            dp[v][i] = add;
            break;
        }
    }
    const int deg = g[v].size();
    vector<ll> gl(deg),gr(deg);
    rep(i,deg){
        gr[i] = (dp[v][i]+1)%m;
        gl[i] = (dp[v][i]+1)%m;
    }
    for(int i = 1; i< deg; i++){
        gl[i] *= gl[i-1];
        gl[i] %=m;
    }
    for(int i = deg-2; i>=0; i--){
        gr[i] *= gr[i+1];
        gr[i] %=m;
    }
    rep(i,deg){
        if(g[v][i] == p) continue;
        ll add_c = 1;
        if(i!=0){
            add_c *= gl[i-1];
            add_c %=m;
        }
        if(i!=deg-1){
            add_c *= gr[i+1];
            add_c %=m;
        }
        Rev(v,g[v][i],add_c);
    }
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cin >> n >> m;
    g.resize(n);
    rep(i,n-1){
        int a,b;
        cin >> a >> b;
        a--; b--;
        g[a].emplace_back(b);
        g[b].emplace_back(a);
    }
    dp.resize(n);
    rep(i,n) dp[i].resize(g[i].size());
    dfs(-1,0);
    Rev(-1,0,0);
    rep(i,n){
        ll ans = 1;
        for(ll add : dp[i]){
            ans *= (add+1);
            ans %=m;
            debug(add);
        }
        cout << ans << '\n';
    }
}

Measured memory_bytes: 22272