#include <bits/stdc++.h>

using namespace std;

// Header files, namespaces,
// macros as defined above
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>


#define f first
#define s second
#define db long double
#define pb push_back
#define sz(c) (c).size()
#define all(c) (c).begin(),(c).end()

typedef long long ll;
typedef pair <int , int> ii;
typedef pair <ii , int> iii;
typedef vector <int> vi;
typedef vector <ii> vii;
typedef vector <iii> viii;
typedef vector <vi> vvi;
typedef vector <vii> vvii;

int mod;
const int MAX = 1e6+5;
const db eps = 1e-13;
const ll oo = 1e18;

// basic operations
int mul( int a , int b ) {
    return 1ll*a*b%mod;
}
int add( int a , int b ) {
    return (a+b)%mod;
}

vvi g , children , preffix_mul , suffix_mul;
vi ans , v , dp;



void dfs( int node , int p = -1 ) {

    for( int nw : g[node] ) {
        if( nw!=p ) {
            children[node].pb( nw );
            preffix_mul[node].pb( 1 );
            suffix_mul[node].pb( 1 );
            dfs( nw , node );
            dp[node] = mul( dp[node] , dp[nw]+1 );
        }
    }

}

void dfs_rooting( int node, int p_value=-1 ) {
    // pref_part
    for( int i = 0 ; i < sz(children[node]) ; i++ ) {
        int nw = children[node][i];
        if( i>0 )
        {
            preffix_mul[node][i] = mul( preffix_mul[node][i-1] , dp[nw]+1 );
        }
    }
    //suf_part
    for( int i = (int)sz(children[node])-1 ; i >= 0 ; i-- ) {
        int nw = children[node][i];
        if( i+1<sz(children[node]) )
        {
            suffix_mul[node][i] = mul( suffix_mul[node][i+1] , dp[nw]+1 );
        }
    }

    if( node==1 ) {

        ans[node] = dp[node];int i = 0;
        for( int nw: children[node] ) {
            dfs_rooting( nw , mul(preffix_mul[node][i],suffix_mul[node][i]) );
            i++;
        }

    } else {
//        if( node==2 ) {
//            cout<<dp[node]<<" "<<p_value<<"\n";
//        }
        ans[node] = mul( dp[node] , p_value+1 );
        int i = 0;
        for( int nw: children[node] ) {
            dfs_rooting( nw , mul( mul(preffix_mul[node][i],suffix_mul[node][i]), p_value+1 ) );
            i++;
        }

    }

}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
    cout<<setprecision(0)<<fixed;

    int n;cin>>n>>mod;
    g.assign(n+1, v);
    children.assign(n+1, v);
    preffix_mul.assign(n+1, v);
    suffix_mul.assign(n+1, v);
    ans.assign(n+1, 1);
    dp.assign(n+1, 1);

    for( int i = 1; i < n ; i++ )
    {
        int a , b;cin>>a>>b;
        g[a].pb(b);
        g[b].pb(a);
    }

    dfs( 1 );
    dfs_rooting( 1 );

    for( int i = 1; i <= n ; i++ )
        cout<<ans[i]%mod<<"\n";

}
