#include<bits/stdc++.h>
using namespace std;
 
#define ll long long
 
#define fr(i,a,b) for (ll i = (a), _b = (b); i <= _b; i++)
#define frr(i,a,b) for (ll i = (a), _b = (b); i >= _b; i--)
#define rep(i,n) for (ll i = 0, _n = (n); i < _n; i++)
#define repr(i,n) for (ll i = n - 1; i >= 0; i--)
#define fill(ar,val) memset(ar, val, sizeof(ar))
#define fill0(ar) fill((ar), 0)
#define debug(x) cout<<#x<<": "<<x<<endl
 
#define ld double
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
 
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<ii> vii;
typedef vector<int> vi;
 
#define INF 1000000000000000000

ll n;
ll m;
vector<ll> ed[100001];
ll down[100001];
ll up[100001];

ll gcdExtended(ll a, ll b, ll *x, ll *y) 
{ 
    if (a == 0) 
    { 
        *x = 0, *y = 1; 
        return b; 
    } 
  
    ll x1, y1; 
    ll gcd = gcdExtended(b%a, a, &x1, &y1); 
  
    *x = y1 - (b/a) * x1; 
    *y = x1; 
  
    return gcd; 
} 

ll modInverse(ll a, ll m) 
{ 
    ll x, y; 
    ll g = gcdExtended(a, m, &x, &y); 
    ll res = (x%m + m) % m; 
    return res; 
} 

void DFS1(ll src, ll par)
{
    down[src] = 1;

    rep(i,ed[src].size())
    {
        if(ed[src][i]!=par)
        {
            DFS1(ed[src][i],src);
            down[src] = ((down[src]%m)*((down[ed[src][i]]+1)%m))%m;
        }
    }
    // cout << down[src] << endl;
}

void DFS2(ll src, ll par, ll xx)
{
    ll x = ((((xx%m) * ((up[par])%m))%m)+1)%m;
    up[src] = x;

    // cout << src << " " << xx << endl;
    vector<ll> r;
    vector<ll> l;
    l.pb(1);
    ll prev = 1;
    bool hu = 0;

    rep(i,ed[src].size()-1)
    {
        if(!hu && i==ed[src].size()-2)
            break;

        if(ed[src][i]!=par)
        {
            ll xxx = ((prev%m)*((down[ed[src][i]]+1)%m))%m;
            prev = xxx;
            l.pb(xxx);
        }
        else
        {
            hu = 1;
        }
    }
    r.pb(1);
    prev = 1;
    hu = 0;
    frr(i,ed[src].size()-1,1)
    {
        if(!hu && i==1)
            break;

        if(ed[src][i]!=par)
        {
            ll xxx = ((prev%m)*((down[ed[src][i]]+1)%m))%m;
            prev = xxx;
            r.pb(xxx);

            // cout << ed[src][i] << " " << "RRRR" << endl;
        }
        else
            hu = 1;
    }

    ll p1 = 0;
    // rep(i,l.size())
    // {
    //     cout << l[i] << " " << src << endl;
    // }
    // rep(i,r.size())
    // {
    //     cout << r[i] << " " << src << endl;
    // }
    rep(i,ed[src].size())
    {
        if(ed[src][i]!=par)
        {
            // cout << p1 << " " << l[p1] << " " << src << " " << r[p1] << endl;
            DFS2(ed[src][i],src,((l[p1]%m)*(r[ed[src].size()-p1-2]%m))%m);
            p1++;
        }
    }
}

// void DFS2(ll src, ll par, ll parpar)
// {
//     ll x = down[par];
//     ll y = modInverse(down[src]+1,m);
//     cout << x << " " << y << endl;
//     x = (x*y)%m;
//     // rep(i,ed[par].size())
//     // {
//     //     if(ed[par][i]!=parpar && ed[par][i]!=src)
//     //     {
//     //         x = ((x%m)*((down[ed[par][i]]+1)%m)%m);
//     //     }
//     // }
//     // cout << x << " " << up[par] << " " << src << endl;
//     x = ((((x%m) * ((up[par])%m))%m)+1)%m;
//     up[src] = x;
//     // cout << up[src] << endl;
//     rep(i,ed[src].size())
//     {
//         if(ed[src][i]!=par)
//         {
//             DFS2(ed[src][i],src,par);
//         }
//     }
// }

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n >> m;

    rep(i,n-1)
    {
        ll x,y;
        cin >> x >> y;

        ed[x].pb(y);
        ed[y].pb(x);
    }
    ed[1].pb(0);
    down[1] = 1;
    DFS1(1,0);
    DFS2(1,0,1);

    fr(i,1,n)  
    {
        // cout << down[i] << " " << up[i] << endl;
        cout << ((down[i]%m)*(up[i]+m)%m)%m << endl;
    }
}

Measured memory_bytes: 24192