Problem p03181 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(), x.end()
#define rep(i, n) for (int i = 0; i < n; i++)
#define debug(v)                                                               \
  cout << #v << ":";                                                           \
  for (auto x : v) {                                                           \
    cout << x << ' ';                                                          \
  }                                                                            \
  cout << endl;
#define INF 1000000000
// #define mod 1000000007
using ll = long long;
const ll LINF = 1001002003004005006ll;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

// 部分木の数え上げ

ll mod, n;
vector<vector<int>> g;
vector<ll> dp, par, ans;

ll dfs(int pre, int now) {
  ll ret = 1;
  for (auto to : g[now])
    if (to != pre) {
      par[to] = now;
      ret = ret * (1 + dfs(now, to)) % mod;
    }
  return dp[now] = ret;
}

void fill_dp(int now) {
  // cout<<now<<" : "<<dp[now]<<endl;
  int m = (int)g[now].size();
  vector<ll> l(m), r(m);
  l[0] = dp[g[now][0]] + 1;
  r[m - 1] = dp[g[now][m - 1]] + 1;
  for (int i = 1; i < m; i++)
    l[i] = l[i - 1] * (dp[g[now][i]] + 1) % mod;
  for (int i = m - 2; i >= 0; i--)
    r[i] = r[i + 1] * (dp[g[now][i]] + 1) % mod;
  ans[now] = 1;
  for (auto to : g[now])
    ans[now] = ans[now] * (dp[to] + 1) % mod;
  rep(i, m) if (par[now] != g[now][i]) {
    dp[now] = 1;
    if (i > 0)
      dp[now] = dp[now] * l[i - 1] % mod;
    if (i < m - 1)
      dp[now] = dp[now] * r[i + 1] % mod;
    fill_dp(g[now][i]);
  }
}

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

  cin >> n >> mod;
  g.resize(n);
  dp.resize(n, 0);
  par.resize(n, -1);
  ans.resize(n);
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }

  dfs(-1, 0);

  ans[0] = dp[0];

  fill_dp(0);

  // debug(par);
  // debug(dp);debug(ans);

  rep(i, n) cout << ans[i] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(), x.end()
#define rep(i, n) for (int i = 0; i < n; i++)
#define debug(v)                                                               \
  cout << #v << ":";                                                           \
  for (auto x : v) {                                                           \
    cout << x << ' ';                                                          \
  }                                                                            \
  cout << endl;
#define INF 1000000000
// #define mod 1000000007
using ll = long long;
const ll LINF = 1001002003004005006ll;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

// 部分木の数え上げ

ll mod, n;
vector<vector<int>> g;
vector<ll> dp, par, ans;

ll dfs(int pre, int now) {
  ll ret = 1;
  for (auto to : g[now])
    if (to != pre) {
      par[to] = now;
      ret = ret * (1 + dfs(now, to)) % mod;
    }
  return dp[now] = ret;
}

void fill_dp(int now) {
  // cout<<now<<" : "<<dp[now]<<endl;
  int m = (int)g[now].size();
  vector<ll> l(m), r(m);
  l[0] = dp[g[now][0]] + 1;
  r[m - 1] = dp[g[now][m - 1]] + 1;
  for (int i = 1; i < m; i++)
    l[i] = l[i - 1] * (dp[g[now][i]] + 1) % mod;
  for (int i = m - 2; i >= 0; i--)
    r[i] = r[i + 1] * (dp[g[now][i]] + 1) % mod;
  ans[now] = 1;
  for (auto to : g[now])
    ans[now] = ans[now] * (dp[to] + 1) % mod;
  rep(i, m) if (par[now] != g[now][i]) {
    dp[now] = 1;
    if (i > 0)
      dp[now] = dp[now] * l[i - 1] % mod;
    if (i < m - 1)
      dp[now] = dp[now] * r[i + 1] % mod;
    fill_dp(g[now][i]);
  }
}

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

  cin >> n >> mod;
  if (n == 1) {
    cout << 1 << endl;
    return 0;
  }
  g.resize(n);
  dp.resize(n, 0);
  par.resize(n, -1);
  ans.resize(n);
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }

  dfs(-1, 0);

  ans[0] = dp[0];

  fill_dp(0);

  // debug(par);
  // debug(dp);debug(ans);

  rep(i, n) cout << ans[i] << endl;
  return 0;
}