Problem p03181 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
ll n, mod;
const int nax = 1e5 + 5;
ll down[nax];
vector<vector<int>> v(nax);
ll up[nax];
int vis[nax] = {0};
ll ans[nax];
void dfs2(int ci) {
  vis[ci] = 1;
  ll g = up[ci];
  for (auto x : v[ci]) {
    if (vis[x] == 1)
      continue;
    dfs2(x);
    g = (g * down[x]) % mod;
  }
  ans[ci] = g;
}
void dfs(int ci) {
  vis[ci] = 1;
  ll val = 1;
  for (auto x : v[ci]) {
    if (vis[x] == 1)
      continue;
    dfs(x);
    val = (val * down[x]) % mod;
  }
  val++;
  val %= mod;
  down[ci] = val;
}
void dfs1(int ci) {
  vis[ci] = 1;
  if (ci == 0)
    up[ci] = 1;
  vector<int> t;
  for (auto x : v[ci]) {
    if (vis[x] == 1)
      continue;
    t.push_back(x);
  }
  vector<ll> pre(t.size()), suff(t.size());
  for (int i = 0; i < t.size(); i++) {
    pre[i] = down[t[i]];
    if (i > 0)
      pre[i] = (pre[i] * pre[i - 1]) % mod;
  }
  for (int i = (int)t.size() - 1; i >= 0; i--) {
    suff[i] = down[t[i]];
    if (i < (int)t.size() - 1)
      suff[i] = (suff[i] * suff[i + 1]) % mod;
  }
  for (int i = 0; i < t.size(); i++) {
    ll g = 1;
    if (i - 1 >= 0)
      g *= pre[i - 1];
    if (i + 1 <= (int)t.size() - 1)
      g = (g * suff[i + 1]) % mod;
    up[t[i]] = (up[ci] * g + 1) % mod;
    dfs1(t[i]);
  }
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> mod;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  dfs(0);
  for (int i = 0; i < n; i++)
    vis[i] = 0;
  dfs1(0);
  for (int i = 0; i < n; i++)
    vis[i] = 0;
  dfs2(0);
  for (int i = 0; i < n; i++)
    cout << ans[i] << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
ll n, mod;
const int nax = 1e5 + 5;
ll down[nax];
vector<vector<int>> v(nax);
ll up[nax];
int vis[nax] = {0};
ll ans[nax];
void dfs2(int ci) {
  vis[ci] = 1;
  ll g = up[ci];
  for (auto x : v[ci]) {
    if (vis[x] == 1)
      continue;
    dfs2(x);
    g = (g * down[x]) % mod;
  }
  ans[ci] = g;
}
void dfs(int ci) {
  vis[ci] = 1;
  ll val = 1;
  for (auto x : v[ci]) {
    if (vis[x] == 1)
      continue;
    dfs(x);
    val = (val * down[x]) % mod;
  }
  val++;
  val %= mod;
  down[ci] = val;
}
void dfs1(int ci) {
  vis[ci] = 1;
  if (ci == 0)
    up[ci] = 1;
  vector<int> t;
  for (auto x : v[ci]) {
    if (vis[x] == 1)
      continue;
    t.push_back(x);
  }
  vector<ll> pre(t.size()), suff(t.size());
  for (int i = 0; i < t.size(); i++) {
    pre[i] = down[t[i]];
    if (i > 0)
      pre[i] = (pre[i] * pre[i - 1]) % mod;
  }
  for (int i = (int)t.size() - 1; i >= 0; i--) {
    suff[i] = down[t[i]];
    if (i < (int)t.size() - 1)
      suff[i] = (suff[i] * suff[i + 1]) % mod;
  }
  for (int i = 0; i < t.size(); i++) {
    ll g = 1;
    if (i - 1 >= 0)
      g *= pre[i - 1];
    if (i + 1 <= (int)t.size() - 1)
      g = (g * suff[i + 1]) % mod;
    up[t[i]] = (up[ci] * g + 1) % mod;
    dfs1(t[i]);
  }
}
int main() {
  /*
  #ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
  #endif
  */
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> mod;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  dfs(0);
  for (int i = 0; i < n; i++)
    vis[i] = 0;
  dfs1(0);
  for (int i = 0; i < n; i++)
    vis[i] = 0;
  dfs2(0);
  for (int i = 0; i < n; i++)
    cout << ans[i] << "\n";
}