#include<bits/stdc++.h>
                            using namespace std;

                            #define FOR(i,a,b) for(int i = (a); i < (b); i++)
                            #define rep(i,n) FOR(i,0,n)
                            #define RFOR(i,b,a) for(int i = (b) - 1; i>= (a); i--)
                            #define ITER(it, a) for(typeof(a.begin()) it = a.begin(); it != a.end(); it++)
                            #define FILL(a, value) memset(a, value, sizeof(a)

                            #define SZ(a) (int)a.size()
                            #define ALL(a) a.begin(), a.end()
                            #define PB push_back
                            #define MP make_pair
                            #define er erase
                            #define in insert
                            #define f first
                            #define s second
                            #define sqr(x) ((x)*(x))





                 typedef  long long LL;
                            typedef vector<int> VI;
                            typedef vector<LL > VL;
                            typedef vector<vector<int > > VVI;
                            typedef vector<vector<LL>> VVL;
                            typedef vector <vector<pair<int, int >> >VVPI;
                            typedef vector<vector<pair<LL, LL >> >VVPL;
                            typedef vector<pair<LL, LL >> PL;
                            typedef vector<PL> VPL;
                            typedef pair<int, int> PII;
                            typedef pair<LL,LL > PLL ;
                            typedef vector<PII> VPI;

                            const double PI = acos(-1.0);
                            const int INF = 1e9 * 2;
                            const LL LINF = (LL)1e18 ;

                            const double EPS = 1e-7;
                            const int MAS =  3 *1e5 + 10;
                         //   const int mod = 998244353;
                            const int MOD =1e9+7;
                            const int LOG= 19;
                            const int ar = 1e5 +100;
                            const int MAX = 1e6 + 1e2;
                            const int root = 0;


                          int n,mod;
                          const int nax = 1e5 + 10 ;
                          vector<vector<int >> g;
                          VI children[nax];
                          vector<PII > downs[nax],pref[nax],suf[nax];
                          PII down[nax],up[nax];
                          int add (int a, int b)
                          {
                              return  (a + b )%mod;
                          }
                          int mul(int a, int b)
                          {
                              return a * 1ll * b % mod;
                          }
                          PII merge(PII a, PII  b)
                          {
                              PII p;
                              p.f =mul(a.f,b.f);
                              p.s = add(mul(a.s,add(b.f,b.s)),mul(a.f,b.s));
                              return p ;
                          }
                          PII f(PII x)
                          {
                              return {x.f,add(x.f,x.s)};
                          }
                          // {black,white}
                          void dfs_one(int v ,int p = -1)
                          {
                              PII st = {1,0};
                              for(auto to : g[v])
                              {
                                  if(to == p)continue;
                                  children[v].PB(to);
                                  dfs_one(to,v);
                                  st = merge(st,down[to]);
                                  downs[v].PB(down[to]);
                              }
                              down[v] = f(st);
                          }
                          void dfs_two(int v ,int parent = - 1 )
                          {
                               rep(i,children[v].size()) {
                                   PII p = {1, 0};
                                   if (i != 0) {
                                       p = merge(p, pref[v][i - 1]);
                                   }
                                   if (i != downs[v].size() - 1) {
                                       p = merge(p, suf[v][i + 1 ]);
                                   }
                                   if (v != 1) {
                                       p = merge(p, up[v]);
                                   }
                                   int to = children[v][i];
                                   up[to] = f(p);
                                   dfs_two(to,v);
                               }

                          }


                            int main() {
                                ios_base::sync_with_stdio(false);
                                cin.tie(NULL);
                                // freopen("horrible.in","r",stdin);
                                // freopen("horrible.out","w",stdout);
                                cin >> n >> mod;
                                g.resize(n + 1 );
                                rep(i,n - 1)
                                {
                                    int x,y;
                                    cin >> x >> y ;
                                    g[x].PB(y);
                                    g[y].PB(x);
                                }
                                dfs_one(1);
                                FOR(i,1,n +1)
                                {
                                    int k = SZ(downs[i]);
                                    PII p = {1,0};
                                    rep(j,k)
                                    {
                                        p = merge(p,downs[i][j]);
                                        pref[i].PB(p);
                                    }
                                    p = {1,0};
                                    RFOR(j,k,0)
                                    {
                                        p = merge(p,downs[i][j]);
                                        suf[i].PB(p);
                                    }
                                    reverse(ALL(suf[i]));
                                }
                                dfs_two(1);
                              FOR(i,1,n + 1)
                                {
                                    PII p = {1,0};
                                    if(i!=1)
                                    {
                                        p = merge(p,up[i]) ;
                                    }
                                    if(SZ(children[i]))
                                    {
                                        p = merge(p,pref[i].back());
                                    }
                                    p = f(p) ;
                                    cout << p.s << '\n';
                                }


                            }


Measured memory_bytes: 35328