#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 parent[maxn], child[maxn], dp1[maxn], mod;
VI pre[maxn], suf[maxn], dp2[maxn], G[maxn];

int getpre(const int u, const int i)
{ return i >= 0 ? pre[u][i] : 1; }
int getsuf(const int u, const int i)
{ return suf[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++;
        }
        pre[u] = suf[u] = VI(n+1,1);
        int i = 0;
        FOR_ALL(v,G[u]) if (*v != p) {
            pre[u][i] = 1LL * getpre(u,i-1) * dp1[*v] % mod;
            pre[u][i] += getpre(u,i-1);
            pre[u][i] %= mod;
            i++;
        }
        i = n-1;
        FOR_ALLR(v,G[u]) if (*v != p) {
            suf[u][i] = 1LL * getsuf(u,i+1) * dp1[*v] % mod;
            suf[u][i] += getsuf(u,i+1);
            suf[u][i] %= mod;
            i--;
        }
        ans = getpre(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 * 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) {
        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