Language: C++

#include <bits/stdc++.h>
#define FOR(v, a, b) for(int v = (a); v < (b); ++v)
#define FORE(v, a, b) for(int v = (a); v <= (b); ++v)
#define REP(v, n) FOR(v, 0, n)
#define REPE(v, n) FORE(v, 0, n)
#define REV(v, a, b) for(int v = (a); v >= (b); --v)
#define ALL(x) (x).begin(), (x).end()
#define ITR(it, c) for(auto it = (c).begin(); it != (c).end(); ++it)
#define RITR(it, c) for(auto it = (c).rbegin(); it != (c).rend(); ++it)
#define EXIST(c,x) ((c).find(x) != (c).end())
#define LLI long long int
#define fst first
#define snd second
#ifdef DEBUG
#include <boost/core/demangle.hpp>
#define dump(x) cerr << "L" << __LINE__ << ": in " << __PRETTY_FUNCTION__ << " \e[32;1m" << boost::core::demangle(typeid(x).name()) << "\e[37m" << " " << (#x) << " = " << (x) << "\e[m" << endl;
#else
#define dump(x)
#endif
using namespace std;
template <typename T> using V = vector<T>;
template <typename T, typename U> using P = pair<T,U>;
template <typename I> void join(ostream &ost, I s, I t, string d=" "){for(auto i=s; i!=t; ++i){if(i!=s)ost<<d; ost<<*i;}ost<<endl;}
template <typename T> istream& operator>>(istream &is, vector<T> &v){for(auto &a : v) is >> a; return is;}
template <typename T, typename U> istream& operator>>(istream &is, pair<T,U> &p){is >> p.first >> p.second; return is;}

template <typename T> ostream& operator<<(ostream &os, vector<T> &v){os << "{";ITR(i,v){if(i!=v.begin())os << ","; os << *i;}os << "}"; return os;}
template <typename T, typename U> ostream& operator<<(ostream& os, pair<T,U> &p){os << "(" << p.first << "," << p.second << ")"; return os;}

template <typename T> T& chmin(T &a, const T &b){return a = min(a,b);}
template <typename T> T& chmax(T &a, const T &b){return a = max(a,b);}

template <typename T = int> T minimum_power_2(T n){
  T i = 1;
  while(i<n) i = (i<<1);
  return i;
}

template <typename T> class SegmentTree{
private:
  int size;
  vector<T> vec;
  T e;
  function<T(T,T)> f, upd;
  
  T aux(int x, int y, int i, int l, int r){
    if(r<=x || y<=l) return e;
    else if(x<=l && r<=y) return vec[i];
    else return f(aux(x,y,i*2+1,l,(l+r)/2), aux(x,y,i*2+2,(l+r)/2,r));
  };

public:
  SegmentTree(){}
  SegmentTree(int n, T e, function<T(T,T)> f, function<T(T,T)> upd):
    size(minimum_power_2(n)*2-1), vec(size, e), f(f), e(e), upd(upd){}
  void update(int i, T x){
    int j = i+(size+1)/2-1;
    vec[j] = upd(vec[j], x);
    --j;
    while(j>=0){
      vec[j/2] = f(vec[(j/2)*2+1], vec[(j/2)*2+2]);
      (j /= 2) -= 1;
    }
  }

  T query(int x, int y){return aux(x,y,0,0,(size+1)/2);}
};

LLI N,M;

class mod_int_64{
public:
  int64_t val;
  mod_int_64(): val(0){}
  mod_int_64(int64_t n): val(n){}
  mod_int_64(int32_t n): val(n){}
  
  const mod_int_64 operator+(const mod_int_64 &a) const {return mod_int_64((val+a.val)%M);}
  const mod_int_64 operator-(const mod_int_64 &a) const {return mod_int_64((val-a.val+M)%M);}
  const mod_int_64 operator*(const mod_int_64 &a) const {return mod_int_64((val*a.val)%M);}
  
  const mod_int_64& operator=(const mod_int_64 &a){val = a.val; return *this;}
  const mod_int_64& operator+=(const mod_int_64 &a){(val += a.val) %= M; return *this;}
  const mod_int_64& operator-=(const mod_int_64 &a){((val -= a.val) += M) %= M; return *this;}
  const mod_int_64& operator*=(const mod_int_64 &a){(val *= a.val) %= M; return *this;}
};

istream& operator>>(istream &is, mod_int_64 &a){is >> a.val; return is;}
ostream& operator<<(ostream &os, const mod_int_64 &a){ os << a.val; return os;}

using mint = mod_int_64;

mint dp[101000];
mint dp2[101000];
vector<bool> visit(101000);
vector<SegmentTree<mint>> prod;

mint rec(vector<vector<int>> &tree, int cur, int par){
  if(visit[cur]) return dp[cur];
  visit[cur] = true;

  dp[cur] = 1;

  for(auto next : tree[cur]){
    if(next == par) continue;
    dp[cur] *= (rec(tree,next,cur)+1);
  }

  int c = tree[cur].size();
  if(par != -1) --c;

  prod[cur] = SegmentTree<mint>(c,1,[&](mint a, mint b){return a*b;}, [&](mint a, mint b){return b;});
  
  REP(i,c){
    prod[cur].update(i,dp[tree[cur][i]]+1);
  }
  
  return dp[cur];
}

mint rec2(vector<vector<int>> &tree, int cur, int par, int index){
  if(visit[cur]) return dp2[cur];
  visit[cur] = true;

  if(par == -1) dp2[cur] = 0;
  else dp2[cur] = (dp2[par]+1) * prod[par].query(0,index) * prod[par].query(index+1,tree[par].size());

  int i = 0;
  for(auto next : tree[cur]){
    if(next == par) continue;
    rec2(tree, next, cur, i);
    ++i;
  }
  
  return dp2[cur];
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> N >> M;
  vector<vector<int>> tree(N);
  REP(i,N-1){
    int x,y; cin >> x >> y;
    --x, --y;
    tree[x].push_back(y);
    tree[y].push_back(x);
  }

  prod.resize(N);

  rec(tree, 0, -1);
  fill(ALL(visit),false);
  rec2(tree, 0, -1, 0);

  REP(i,N){
    mint ans = (dp2[i]+1) * dp[i];
    cout << ans << endl;
  }
  
  return 0;
}


Compiler output:
a.cc: In member function 'const mod_int_64 mod_int_64::operator+(const mod_int_64&) const':
a.cc:78:89: error: call of overloaded 'mod_int_64(long long int)' is ambiguous
   78 |   const mod_int_64 operator+(const mod_int_64 &a) const {return mod_int_64((val+a.val)%M);}
      |                                                                                         ^
a.cc:76:3: note: candidate: 'mod_int_64::mod_int_64(int32_t)'
   76 |   mod_int_64(int32_t n): val(n){}
      |   ^~~~~~~~~~
a.cc:75:3: note: candidate: 'mod_int_64::mod_int_64(int64_t)'
   75 |   mod_int_64(int64_t n): val(n){}
      |   ^~~~~~~~~~
a.cc:71:7: note: candidate: 'constexpr mod_int_64::mod_int_64(const mod_int_64&)'
   71 | class mod_int_64{
      |       ^~~~~~~~~~
a.cc: In member function 'const mod_int_64 mod_int_64::operator-(const mod_int_64&) const':
a.cc:79:91: error: call of overloaded 'mod_int_64(long long int)' is ambiguous
   79 |   const mod_int_64 operator-(const mod_int_64 &a) const {return mod_int_64((val-a.val+M)%M);}
      |                                                                                           ^
a.cc:76:3: note: candidate: 'mod_int_64::mod_int_64(int32_t)'
   76 |   mod_int_64(int32_t n): val(n){}
      |   ^~~~~~~~~~
a.cc:75:3: note: candidate: 'mod_int_64::mod_int_64(int64_t)'
   75 |   mod_int_64(int64_t n): val(n){}
      |   ^~~~~~~~~~
a.cc:71:7: note: candidate: 'constexpr mod_int_64::mod_int_64(const mod_int_64&)'
   71 | class mod_int_64{
      |       ^~~~~~~~~~
a.cc: In member function 'const mod_int_64 mod_int_64::operator*(const mod_int_64&) const':
a.cc:80:89: error: call of overloaded 'mod_int_64(long long int)' is ambiguous
   80 |   const mod_int_64 operator*(const mod_int_64 &a) const {return mod_int_64((val*a.val)%M);}
      |                                                                                         ^
a.cc:76:3: note: candidate: 'mod_int_64::mod_int_64(int32_t)'
   76 |   mod_int_64(int32_t n): val(n){}
      |   ^~~~~~~~~~
a.cc:75:3: note: candidate: 'mod_int_64::mod_int_64(int64_t)'
   75 |   mod_int_64(int64_t n): val(n){}
      |   ^~~~~~~~~~
a.cc:71:7: note: candidate: 'constexpr mod_int_64::mod_int_64(const mod_int_64&)'
   71 | class mod_int_64{
      |       ^~~~~~~~~~
a.cc: In function 'mint rec(std::vector<std::vector<int> >&, int, int)':
a.cc:99:6: error: reference to 'visit' is ambiguous
   99 |   if(visit[cur]) return dp[cur];
      |      ^~~~~
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:80,
                 from a.cc:1:
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:95:14: note:                 'std::vector<bool> visit'
   95 | vector<bool> visit(101000);
      |              ^~~~~
a.cc:100:3: error: reference to 'visit' is ambiguous
  100 |   visit[cur] = true;
      |   ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:95:14: note:                 'std::vector<bool> visit'
   95 | vector<bool> visit(101000);
      |              ^~~~~
a.cc: In function 'mint rec2(std::vector<std::vector<int> >&, int, int, int)':
a.cc:122:6: error: reference to 'visit' is ambiguous
  122 |   if(visit[cur]) return dp2[cur];
      |      ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:95:14: note:                 'std::vector<bool> visit'
   95 | vector<bool> visit(101000);
      |              ^~~~~
a.cc:123:3: error: reference to 'visit' is ambiguous
  123 |   visit[cur] = true;
      |   ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:95:14: note:                 'std::vector<bool> visit'
   95 | vector<bool> visit(101000);
      |              ^~~~~
a.cc: In function 'int main()':
a.cc:154:12: error: reference to 'visit' is ambiguous
  154 |   fill(ALL(visit),false);
      |            ^~~~~
a.cc:7:17: note: in definition of macro 'ALL'
    7 | #define ALL(x) (x).begin(), (x).end()
      |                 ^
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:95:14: note:                 'std::vector<bool> visit'
   95 | vector<bool> visit(101000);
      |              ^~~~~
a.cc:154:12: error: reference to 'visit' is ambiguous
  154 |   fill(ALL(visit),false);
      |            ^~~~~
a.cc:7:30: note: in definition of macro 'ALL'
    7 | #define ALL(x) (x).begin(), (x).end()
      |                              ^
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:95:14: note:                 'std::vector<bool> visit'
   95 | vector<bool> visit(101000);
      |              ^~~~~