Problem p03181 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define INFLL 2000000000000000000
#define INF 2000000000
#define MOD 1000000007
#define BASE 77747
#define PI acos(-1.0)
#define MAXLEN 29

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef vector<ll> vll;

int n;
vector<int> t[100000];
ll m;
ll dp[100000];
ll ans[100000];

void rec(int cur, int par) {
  dp[cur] = 1;
  for (int i = 0; i < t[cur].size(); i++) {
    if (t[cur][i] == par) {
      swap(t[cur][i], t[cur][t[cur].size() - 1]);
      break;
    }
  }
  if (t[cur][t[cur].size() - 1] == par)
    t[cur].pop_back();

  for (int i = 0; i < t[cur].size(); i++) {
    rec(t[cur][i], cur);
    dp[cur] *= (dp[t[cur][i]] + 1) % m;
    dp[cur] %= m;
  }
}

void solve(int cur, int par, ll pval) {
  ans[cur] = dp[cur] * pval;
  ans[cur] %= m;

  vll le(t[cur].size()), ri(t[cur].size());
  int sz = t[cur].size();

  if (sz > 0) {
    le[0] = dp[t[cur][0]] + 1;
    ri[sz - 1] = dp[t[cur][sz - 1]] + 1;
  }
  for (int i = 1; i < t[cur].size(); i++) {
    le[i] = le[i - 1] * (dp[t[cur][i]] + 1);
    le[i] %= m;
    ri[sz - i - 1] = (dp[t[cur][sz - i - 1]] + 1) * ri[sz - i];
    ri[sz - i - 1] %= m;
  }
  // for (int i = 0; i < t[cur].size(); i++) {
  // 	cout << le[i] << " " << ri[i] << endl;
  // }
  // cout << endl;
  for (int i = 0; i < t[cur].size(); i++) {
    ll left = 1;
    ll right = 1;
    if (i > 0)
      left = le[i - 1];
    if (i < t[cur].size() - 1)
      right = ri[i + 1];
    solve(t[cur][i], cur, ((((pval * left) % m) * right) + 1) % m);
  }
}

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  cin >> n >> m;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    t[b].push_back(a);
    t[a].push_back(b);
  }
  rec(0, -1);
  solve(0, -1, 1);
  for (int i = 0; i < n; i++) {
    printf("%lld\n", ans[i]);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define INFLL 2000000000000000000
#define INF 2000000000
#define MOD 1000000007
#define BASE 77747
#define PI acos(-1.0)
#define MAXLEN 29

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef vector<ll> vll;

int n;
vector<int> t[100000];
ll m;
ll dp[100000];
ll ans[100000];

void rec(int cur, int par) {
  dp[cur] = 1;
  for (int i = 0; i < t[cur].size(); i++) {
    if (t[cur][i] == par) {
      swap(t[cur][i], t[cur][t[cur].size() - 1]);
      break;
    }
  }
  if (t[cur].size() > 0 && t[cur][t[cur].size() - 1] == par)
    t[cur].pop_back();

  for (int i = 0; i < t[cur].size(); i++) {
    rec(t[cur][i], cur);
    dp[cur] *= (dp[t[cur][i]] + 1) % m;
    dp[cur] %= m;
  }
}

void solve(int cur, int par, ll pval) {
  ans[cur] = dp[cur] * pval;
  ans[cur] %= m;

  vll le(t[cur].size()), ri(t[cur].size());
  int sz = t[cur].size();

  if (sz > 0) {
    le[0] = dp[t[cur][0]] + 1;
    ri[sz - 1] = dp[t[cur][sz - 1]] + 1;
  }
  for (int i = 1; i < t[cur].size(); i++) {
    le[i] = le[i - 1] * (dp[t[cur][i]] + 1);
    le[i] %= m;
    ri[sz - i - 1] = (dp[t[cur][sz - i - 1]] + 1) * ri[sz - i];
    ri[sz - i - 1] %= m;
  }
  // for (int i = 0; i < t[cur].size(); i++) {
  // 	cout << le[i] << " " << ri[i] << endl;
  // }
  // cout << endl;
  for (int i = 0; i < t[cur].size(); i++) {
    ll left = 1;
    ll right = 1;
    if (i > 0)
      left = le[i - 1];
    if (i < t[cur].size() - 1)
      right = ri[i + 1];
    solve(t[cur][i], cur, ((((pval * left) % m) * right) + 1) % m);
  }
}

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  cin >> n >> m;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    t[b].push_back(a);
    t[a].push_back(b);
  }
  rec(0, -1);
  solve(0, -1, 1);
  for (int i = 0; i < n; i++) {
    printf("%lld\n", ans[i]);
  }
  return 0;
}