Problem p03181 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep2(i, a, b) for (ll i = (a); i < (b); ++i)
#define debug(x) cout << #x << '=' << x << endl
#define all(v) (v).begin(), (v).end()
const ll MOD = 1e9 + 7;
// const ll MOD=998244353;
const ll INF = 1e9;
const ll IINF = 1e18;
const double EPS = 1e-8;
const double pi = acos(-1);

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

const int MAX_N = 1e5 + 10;

int N;
ll M;
vector<vector<int>> G(MAX_N);
vector<ll> dat(MAX_N, 1), ans(MAX_N, 1);

void dfs1(int v, int p) {
  for (int u : G[v]) {
    if (u == p)
      continue;
    dfs1(u, v);
    dat[v] = dat[v] * ((dat[u] + 1) % M) % M;
  }
}

void dfs2(int v, int p, ll d_par) {
  int n = G[v].size(), now = 1, np;
  map<int, int> m1, m2;
  vector<ll> cs1(n), cs2(n, 1);
  ans[v] = cs1[0] = (d_par + 1) % M;
  rep(i, n) {
    if (G[v][i] == p) {
      np = i;
      continue;
    }
    m1[i] = now - 1;
    cs1[now] = cs1[now - 1] * ((dat[G[v][i]] + 1) % M) % M, ++now;
    ans[v] = ans[v] * ((dat[G[v][i]] + 1) % M) % M;
  }
  now = 1;
  rep(i, n) {
    if (G[v][n - 1 - i] == p)
      continue;
    m2[n - 1 - i] = now - 1;
    cs2[now] = cs2[now - 1] * ((dat[G[v][n - 1 - i]] + 1) % M) % M, ++now;
  }
  rep(i, n) {
    if (G[v][i] == p)
      continue;
    dfs2(G[v][i], v, cs1[m1[i]] * cs2[m2[i]] % M);
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N >> M;
  rep(i, N - 1) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  dfs1(0, -1);
  if (N > 1)
    dfs2(0, -1, 0);
  rep(i, N) cout << ans[i] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep2(i, a, b) for (ll i = (a); i < (b); ++i)
#define debug(x) cout << #x << '=' << x << endl
#define all(v) (v).begin(), (v).end()
const ll MOD = 1e9 + 7;
// const ll MOD=998244353;
const ll INF = 1e9;
const ll IINF = 1e18;
const double EPS = 1e-8;
const double pi = acos(-1);

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

const int MAX_N = 1e5 + 10;

int N;
ll M;
vector<vector<int>> G(MAX_N);
vector<ll> dat(MAX_N, 1), ans(MAX_N, 1);

void dfs1(int v, int p) {
  for (int u : G[v]) {
    if (u == p)
      continue;
    dfs1(u, v);
    dat[v] = dat[v] * ((dat[u] + 1) % M) % M;
  }
}

void dfs2(int v, int p, ll d_par) {
  int n = G[v].size(), now = 1, np;
  map<int, int> m1, m2;
  vector<ll> cs1(n + 1), cs2(n + 1, 1);
  ans[v] = cs1[0] = (d_par + 1) % M;
  rep(i, n) {
    if (G[v][i] == p) {
      np = i;
      continue;
    }
    m1[i] = now - 1;
    cs1[now] = cs1[now - 1] * ((dat[G[v][i]] + 1) % M) % M, ++now;
    ans[v] = ans[v] * ((dat[G[v][i]] + 1) % M) % M;
  }
  now = 1;
  rep(i, n) {
    if (G[v][n - 1 - i] == p)
      continue;
    m2[n - 1 - i] = now - 1;
    cs2[now] = cs2[now - 1] * ((dat[G[v][n - 1 - i]] + 1) % M) % M, ++now;
  }
  rep(i, n) {
    if (G[v][i] == p)
      continue;
    dfs2(G[v][i], v, cs1[m1[i]] * cs2[m2[i]] % M);
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N >> M;
  rep(i, N - 1) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  dfs1(0, -1);
  if (N > 1)
    dfs2(0, -1, 0);
  rep(i, N) cout << ans[i] << endl;
}