#include <bits/stdc++.h>

using namespace std;

/* g++ -g -fsanitize=address -lasan -std=c++11 -Dfoo_ */

/* freopen("input.txt", "rt", stdin); */
/* freopen("output.txt", "wt", stdout); */

#define ALL(c)          (c).begin(), (c).end()
#define ALLR(c)         (c).rbegin(), (c).rend()
#define FOR(i,a,b)      for (int i=(a); i < (b); ++i)
#define FORR(i,a,b)     for (int i=(a); i > (b); --i)
#define FOR_ALL(i,c)    for (__typeof((c).begin()) i=(c).begin();   \
                             i != (c).end(); ++i)
#define FOR_ALLR(i,c)   for (__typeof((c).rbegin()) i=(c).rbegin(); \
                             i != (c).rend(); ++i)
#define SZ(array)       (sizeof(array)/sizeof(array[0]))
#define lc(x)           (x<<1)     /* 2*x */
#define rc(x)           (x<<1 | 1) /* 2*x+1 */
#define lowbit(x)       (x & (-x)) /* 0b10100 -> 0b100 */

typedef long long       LL;
typedef map<int,int>    MII;
typedef pair<int,int>   PII;
typedef set<int>        SI;
typedef vector<bool>    VB;
typedef vector<double>  VD;
typedef vector<int>     VI;
typedef vector<string>  VS;

/* check if a key is in container C */
template <class C>
inline bool in_(const typename C::key_type& k, const C& A)
{ return A.find(k) != A.end(); }
inline bool in_(const string& s, const string& S)
{ return S.find(s) != string::npos; }

const int maxn = 1e5+5;
int size[maxn], parent[maxn], child[maxn], dp1[maxn], mod;
VI pre[maxn], suf[maxn], G1[maxn], G2[maxn]; map<PII,int> dp2;

int dfs0(const int u, const int p)
{
    size[u] = 1; parent[u] = p; int i = 0;
    FOR_ALL(v,G1[u]) if (*v != p) {
        size[u] += dfs0(*v,u);
        child[*v] = i++;
        G2[u].push_back(*v);
    }
    return size[u];
}

int getpre(const int u, const int i)
{ return i >= 0 ? pre[u][i] : 1; }
int getsuf(const int u, const int i)
{ return i < G2[u].size() ? suf[u][i] : 1; }

int dfs1(const int u)
{
    if (size[u] == 1)
        return 1;
    int& ans = dp1[u];
    if (-1 == ans) {
        ans = 1;
        FOR_ALL(v,G2[u]) 
            dfs1(*v);
        const int n = G2[u].size();
        pre[u] = suf[u] = VI(n,1);
        FOR(i,0,n) {
            pre[u][i] = 1LL * getpre(u,i-1) * dfs1(G2[u][i]) % mod;
            pre[u][i] += getpre(u,i-1);
            pre[u][i] %= mod;
        }
        FORR(i,n-1,-1) {
            suf[u][i] = 1LL * getsuf(u,i+1) * dfs1(G2[u][i]) % mod;
            suf[u][i] += getsuf(u,i+1);
            suf[u][i] %= mod;
        }
        ans = pre[u][n-1];
    }
    return ans;
}

int dfs2(const int u, const int c)
{
    if (u == 0)
        return 0;
    const PII k(u,c);
    if (in_(k,dp2))
        return dp2[k];
    int& ans = dp2[k] = 1LL * getpre(u,c-1) * getsuf(u,c+1) % mod;
    ans += 1LL * ans * dfs2(parent[u],child[u]) % mod;
    ans %= mod;
    return ans;
}

int main()
{
#ifdef foo_
    freopen("foo", "rt", stdin);
#endif
    ios::sync_with_stdio(false);
    int n,m;
    while (cin >> n >> m) {
        dp2.clear(); mod = m;
        FOR(i,1,n+1)
            G1[i].clear(), G2[i].clear(), dp1[i] = -1;
        FOR(i,1,n) {
            int u,v; cin >> u >> v;
            G1[u].push_back(v);
            G1[v].push_back(u);
        }
        dfs0(1,0);
        dfs1(1);
        FOR(i,1,n+1) {
            int ans = dfs1(i);
            ans += 1LL * ans * dfs2(parent[i],child[i]) % mod;
            ans %= mod;
            cout << ans << endl;
        }
    }
    return 0;
}


Measured memory_bytes: 37504