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

int n;
vi ans1;

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


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 naive_dfs( int node , int p = -1 ) {

    for( int nw : g[node] ) {
        if( nw!=p ) {
            naive_dfs( nw , node );
            dp1[node] = mul( dp1[node] , dp1[nw]+1 );
        }
    }

}


void naive_solver() {


    ans1.assign(n+1,1);

    for( int i = 1; i <= n ; i++ ) {
        dp1.assign(n+1,1);
        naive_dfs(i);
//        if( i==2 ) {
//            cout<<dp1[1]<<"a\n";
//        }
        ans1[i] = dp1[i];
    }

}


void dfs_rooting( int node, int p_value=0 ) {
    // pref_part
    for( int i = 0 ; i < sz(children[node]) ; i++ ) {

        if( i>0 )
        {
            int nw = children[node][i-1];
            preffix_mul[node][i] = mul( preffix_mul[node][i-1] , dp[nw]+1 );
//            if( node==1 ) {
//                cout<<preffix_mul[node][i]<<" "<<nw<<" "<<dp[nw]<<"p\n";
//            }
        }
    }
    //suf_part
    for( int i = (int)sz(children[node])-1 ; i >= 0 ; i-- ) {

        if( i+1<sz(children[node]) )
        {
            int nw = children[node][i+1];
            suffix_mul[node][i] = mul( suffix_mul[node][i+1] , dp[nw]+1 );
//            if( node==1 ) {
//                cout<<suffix_mul[node][i]<<" "<<nw<<" "<<dp[nw]<<"s\n";
//            }
        }
    }

//    if( node==2 ) {
//        cout<<p_value<<"b\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++;
    }



}



void data_gen() {

    n = (int)rand()%15;
    mod = (int)rand()%1000000000;
    g.assign(n+1, v);
    int tmp = 2;
    vi mk(n+1,0);
    for( int i = 1; i <= n ; i++ ) {

        int cnt = (int)rand()%(n+1);
        int j;
        for(  j = tmp ; j <= min( n , tmp+cnt-(i!=1) ) ; j++ ) {
            g[i].pb(j);
            g[j].pb(i);
        }
        tmp = j;

    }


}

//print() {
//
////    cout<<n<<" "<<mod<<"\n";
////    for( int i = 1 ; i <= n ; i++ ) {
////
////        for( int nw: g[i] ) {
////
////            cout<<i<<" "<<nw<<"\n";
////        }
//////        cout<<"\n";
////    }
//
//    for( int i = 1 ; i <= n ; i++ ) {
//        cout<<ans[i]<<" "<<ans1[i]<<" "<<i<<"\n";
//    }
//
//}

bool check() {
    for( int i = 1; i <= n ; i++ )
        if( ans[i] != ans1[i] )
            return false;
    return true;
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
    cout<<setprecision(0)<<fixed;
//    for( int t = 0 ; t < 10000 ; t++ )  {
        cin>>n>>mod;
//        data_gen();
        children.assign(n+1, v);
        g.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 );
//        naive_solver();
//        if( !check() ) {
//            print();
//        }
        for( int i = 1; i <= n ; i++ )
            cout<<ans[i]<<"\n";
//    }
}
/*
8 23811
1 2
1 3
1 4
2 5
2 6
2 7
3 8
*/

Measured memory_bytes: 28160