#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; }

/* For each vetex v, count # of subtrees can be rooted at v. */
/*
 *         r        Suppose the tree is rooted at r, we use dfs1 to get
 *        / \       value of subtree rooted at v. But when we reroot
 *       u   ..     the tree at v, we need to consider the value at u
 *      /|\         and all the other upper part of the tree. When
 *    /| | |\       consider u, we need to a merge of node 1,2 and 4,5.
 *   1 2[v]4 5      We can use a prefix and suffix value to calculate
 *   prx/ \sfx      the combination of node 1,2,4,5, where 3 is current
 *     /   \        node v.
 */

const int maxn = 1e5+5;
int parent[maxn], child[maxn], dp1[maxn], mod;
VI prx[maxn], sfx[maxn], dp2[maxn], G[maxn];

int getprx(const int u, const int i)
{ return i >= 0 ? prx[u][i] : 1; }
int getsfx(const int u, const int i)
{ return sfx[u][i]; }

int dfs1(const int u, const int p)
{
    parent[u] = p;
    int& ans = dp1[u];
    if (-1 == ans) {
        int n = 0;
        FOR_ALL(v,G[u]) if (*v != p)
            dfs1(*v,u), child[*v] = n++;
        prx[u] = sfx[u] = VI(n+1,1); /* +1 for sentinel */
        int i = 0;
        FOR_ALL(v,G[u]) if (*v != p) {
            /* use v */
            prx[u][i] = 1LL * getprx(u,i-1) * dp1[*v] % mod;
            /* not use v */
            prx[u][i] += getprx(u,i-1);
            prx[u][i] %= mod;
            i++;
        }
        i = n-1;
        FOR_ALLR(v,G[u]) if (*v != p) {
            sfx[u][i] = 1LL * getsfx(u,i+1) * dp1[*v] % mod;
            sfx[u][i] += getsfx(u,i+1);
            sfx[u][i] %= mod;
            i--;
        }
        ans = getprx(u,n-1);
    }
    return ans;
}

int dfs2(const int u, const int c)
{
    if (u == 0)
        return 0;
    if (dp2[u].empty())
        dp2[u] = VI(G[u].size(),-1);
    int& ans = dp2[u][c];
    if (-1 == ans) {
        ans  = 1LL * getprx(u,c-1) * getsfx(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) {
        mod = m;
        FOR(i,1,n+1)
            G[i].clear(), dp1[i] = -1, dp2[i].clear();
        FOR(i,1,n) {
            int u,v; cin >> u >> v;
            G[u].push_back(v);
            G[v].push_back(u);
        }
        dfs1(1,0);
        FOR(i,1,n+1) {
            int ans = dp1[i];
            ans += 1LL * ans * dfs2(parent[i],child[i]) % mod;
            ans %= mod;
            cout << ans << endl;
        }
    }
    return 0;
}


Measured memory_bytes: 30464