#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

#define inf 1013161010
#define ninf -1013161010
#define mod 1000000007
#define ll long long
#define lf long double
#define in(x) scanf("%d",&x);
#define sz(x) ((int)x.size())
#define lld l64d
#define rep(i,n) for(i=0;i<n;i++)
#define rrep(i,n) for(i=n-1;i>=0;i--)
#define rep1(i,a,b) for(i=a;i<=b;i++)
#define rrep1(i,a,b) for(i=a;i>=b;i--)
#define stlfor(i,t) for(auto i =t.begin();i!=t.end();i++)
#define fr freopen("x.txt","r",stdin)
#define frc freopen("y.txt","w",stdout)
#define all(x) x.begin(),x.end()
#define set0(x) memset(x,0,sizeof(x))
#define dbg cout<<"yo "<<endl;
#define pset(n) fixed<<showpoint<<setprecision(n)

#define pii pair<int,int>
#define pll pair<ll,ll>
#define min_pq priority_queue<pll, vector<pll>, greater<pll> >
#define vpii vector<pair<int,int> >
#define vll vector<ll>
#define vpll vector<pair<ll,ll> >
#define si set<int>
#define mii map<int,int>
#define umii unordered_map<int,int>
#define vi vector<int>
#define pb push_back
#define ff first
#define ss second
#define mp make_pair
#define modulo %
#define moduloe %=

typedef tree<pii,null_type,less<pii>,rb_tree_tag,tree_order_statistics_node_update> OST;

ll toint(const string &s) { stringstream ss; ss << s; ll x; ss >> x; return x; }
ll add(ll a, ll b) {a+=b; while(a >= mod) a-= mod; while(a<0) a+= mod;}
ll mul(ll a, ll b) {return (a * 1LL *b % mod);}
string tostring ( ll number ){  stringstream ss; ss<< number; return ss.str();}

const lf pi = 2*acos(0);
const int nn = 500006;
const lf EPS = 0.000000001;

ll gcd(ll a,ll b){return (b==0)? a:gcd(b,a%b); }
void nope(int num = 0){ if(num==0) cout<<"NO"; else cout<<"-1"; exit(0); }

ll f[nn], pl[nn], pr[nn], upar[nn];
vi g[nn];
ll m;

void dfs1(int node, int par)
{
    f[node] = 1LL;
    for(auto u : g[node])
    {
        if(u != par)
        {
            dfs1(u, node);
            f[node] = (f[node] * (f[u] + 1));
            f[node] %= m;
        }
    }
}

void dfs(int node, int par)
{
    if(par == -1)
        upar[node] = 0;

    int i, t = sz(g[node]);
    for(i=0; i<t+1; i++)
    {
        pl[i] = 0;
        pr[i] = 0;
    }

    if(t > 0)
    {
        if(g[node][0] != par)
            pl[0] = f[g[node][0]] + 1LL;
        else
            pl[0] = 1LL;
        if(g[node][t-1] != par)
            pr[t-1] = f[g[node][t-1]] + 1LL;
        else
            pr[t-1] = 1LL;
    }

    pl[0] %= m;
    pr[t-1] %= m;

    for(i=1;i<t;i++)
    {
        if(g[node][i] != par)
        {
            pl[i] = pl[i-1] * (f[g[node][i]]+1LL);
            pl[i] %= m;
        }
        else
            pl[i] = pl[i-1];
    }

    for(i=t-2;i>=0;i--)
    {
        if(g[node][i] != par)
        {
            pr[i] = pr[i+1] * (f[g[node][i]]+1LL);
            pr[i] %= m;
        }
        else
        {
            pr[i] = pr[i+1];
        }
    }

    ll res = upar[node] + 1LL;
    res %= m;

//    if(node == 2)
//    {
//        cout<<g[node][0]<<" "<<g[node][1]<<endl;
//        cout<<t<<endl;
//        rep(i, t)
//            cout<<pl[i]<<" ";
//        cout<<endl;
//        rep(i, t)
//            cout<<pr[i]<<" ";
//        cout<<endl;
//    }
    for(i=0;i<t;i++)
    {
        ll res1;
        if(g[node][i] != par)
        {
            res1 = res;
            if(i > 0)
            {
                res1 = res1 * pl[i-1];
                res1 %= m;
            }
            if(i < t-1)
            {
                res1 = res1 * pr[i+1];
                res1 %= m;
            }

            upar[g[node][i]] = res1;
        }
    }

    for(auto u : g[node])
    {
        if(u != par)
            dfs(u, node);
    }
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(0);
    ll i,j,n,tt;
    tt=1;
    //cin>>tt;
    while(tt--)
    {
        int x, y;
        cin>>n>>m;
        rep(i, n-1)
        {
            cin>>x>>y;
            g[x].pb(y);
            g[y].pb(x);
        }

        dfs1(1, -1);

        dfs(1, -1);

        for(i = 1; i<=n; i++)
        {
            cout<<(((upar[i] + 1) * (f[i])) %m)<<endl;
        }
        cout<<endl;
    }
    return 0;
}

// read the question correctly (is y a vowel? what are the exact constraints?)
// look out for SPECIAL CASES (n=1?) and overflow (ll vs int?)


Measured memory_bytes: 30464