Problem p03181 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

const int MOD = 1e9 + 7;
const long long INF = 1e18;
const int MaxN = 1e5;

int n, mod;

vector<int> v[MaxN];
int dp[MaxN];
vector<int> pref[MaxN], suf[MaxN];
int ans[MaxN];

int pDP[MaxN];

void add(int &x, int y) {
  x += y;
  if (x >= MOD)
    x -= MOD;
}

void prod(int &x, int y) { x = (long long)x * y % mod; }

void dfs(int x, int p = 0) {
  if (p)
    for (int i = 0; i < v[x].size(); ++i)
      if (v[x][i] == p) {
        v[x].erase(v[x].begin() + i);
        break;
      }

  int n = v[x].size();

  pref[x].resize(n);
  suf[x].resize(n);

  for (int i = 0; i < n; ++i) {
    dfs(v[x][i], x);

    pref[x][i] = (1 + dp[v[x][i]]) % mod;

    if (i) {
      prod(pref[x][i], pref[x][i - 1]);
    }
  }
  for (int i = n - 1; i >= 0; --i) {
    suf[x][i] = (1 + dp[v[x][i]]) % mod;

    if (i + 1 < n) {
      prod(suf[x][i], suf[x][i + 1]);
    }
  }

  dp[x] = (suf[x].empty() ? 1 : suf[x][0]);
}

void dfs2(int x, int p = 0, int pos = -1) {
  if (p) {
    pDP[x] = (pDP[p] + 1) % mod;
    if (pos > 0)
      prod(pDP[x], pref[p][pos - 1]);
    if (pos + 1 < v[p].size())
      prod(pDP[x], suf[p][pos + 1]);
  }

  int i = 0;
  for (int to : v[x])
    dfs2(to, x, i++);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  //    freopen("input.txt", "r", stdin);
  cin >> n >> mod;

  for (int i = 1; i < n; ++i) {
    int x, y;
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }

  dfs(1);
  dfs2(1);

  for (int i = 1; i <= n; ++i) {
    int ans = (long long)dp[i] * (1 + pDP[i]) % mod;
    cout << ans << '\n';
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int MOD = 1e9 + 7;
const long long INF = 1e18;
const int MaxN = 1e5 + 15;

int n, mod;

vector<int> v[MaxN];
int dp[MaxN];
vector<int> pref[MaxN], suf[MaxN];
int ans[MaxN];

int pDP[MaxN];

void add(int &x, int y) {
  x += y;
  if (x >= MOD)
    x -= MOD;
}

void prod(int &x, int y) { x = (long long)x * y % mod; }

void dfs(int x, int p = 0) {
  if (p)
    for (int i = 0; i < v[x].size(); ++i)
      if (v[x][i] == p) {
        v[x].erase(v[x].begin() + i);
        break;
      }

  int n = v[x].size();

  pref[x].resize(n);
  suf[x].resize(n);

  for (int i = 0; i < n; ++i) {
    dfs(v[x][i], x);

    pref[x][i] = (1 + dp[v[x][i]]) % mod;

    if (i) {
      prod(pref[x][i], pref[x][i - 1]);
    }
  }
  for (int i = n - 1; i >= 0; --i) {
    suf[x][i] = (1 + dp[v[x][i]]) % mod;

    if (i + 1 < n) {
      prod(suf[x][i], suf[x][i + 1]);
    }
  }

  dp[x] = (suf[x].empty() ? 1 : suf[x][0]);
}

void dfs2(int x, int p = 0, int pos = -1) {
  if (p) {
    pDP[x] = (pDP[p] + 1) % mod;
    if (pos > 0)
      prod(pDP[x], pref[p][pos - 1]);
    if (pos + 1 < v[p].size())
      prod(pDP[x], suf[p][pos + 1]);
  }

  int i = 0;
  for (int to : v[x])
    dfs2(to, x, i++);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  //    freopen("input.txt", "r", stdin);
  cin >> n >> mod;

  for (int i = 1; i < n; ++i) {
    int x, y;
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }

  dfs(1);
  dfs2(1);

  for (int i = 1; i <= n; ++i) {
    int ans = (long long)dp[i] * (1 + pDP[i]) % mod;
    cout << ans << '\n';
  }

  return 0;
}