Problem p03181 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define f first
#define s second
#define pb push_back
ll mod;
long long binpow(long long a, long long b, long long m = mod) {
  a %= m;
  long long res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}
ll modinv(ll n) { return binpow(n, mod - 2, mod); }
vector<ll> ar[100001];
ll dp[100001] = {0};
ll dfs(ll u, ll p) {
  dp[u] = 1ll;
  for (auto it : ar[u]) {
    if (it != p) {
      dfs(it, u);
      dp[u] *= (dp[it] + 1ll);
      dp[u] %= mod;
    }
  }
}
ll ans[100001];
ll dfs2(ll u, ll p) {
  vector<ll> g;
  ll f = 1ll;
  vector<ll> k;
  for (auto it = ar[u].begin(); it != ar[u].end(); ++it) {
    if (*it != p) {
      k.push_back(*it);
      f *= (dp[(*it)] + 1ll);
      f %= mod;
      g.push_back(f);
    }
  }
  deque<ll> r;
  f = 1ll;
  for (auto it = ar[u].rbegin(); it != ar[u].rend(); ++it) {
    if (*it != p) {
      f *= (dp[(*it)] + 1ll);
      f %= mod;
      r.push_front(f);
    }
  }
  for (int x = 0; x < k.size(); x++) {
    ll t = 1;
    if (x > 0)
      t *= g[x - 1];
    t %= mod;
    if (x < k.size() - 1ll)
      t *= r[x + 1];
    t %= mod;
    ans[k[x]] *= (ans[u] * t + 1ll) % mod;
    ans[k[x]] %= mod;
    dfs2(k[x], u);
  }
  ans[u] *= dp[u];
  ans[u] %= mod;
}
void solve() {
  ll n;
  cin >> n >> mod;
  ll u, v;
  for (int x = 1; x <= n; x++)
    ans[x] = 1;
  for (int x = 0; x < n - 1; x++) {
    cin >> u >> v;
    ar[u].pb(v);
    ar[v].pb(u);
  }
  dfs(1, -1);
  dfs2(1, -1);
  for (int x = 1; x <= n; x++) {
    cout << ans[x] << "\n";
  }
}
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);
  ll i = 1;
  // cin>>i;
  ll k = 0;
  while (k < i) {
    solve();
    ++k;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define f first
#define s second
#define pb push_back
ll mod;
long long binpow(long long a, long long b, long long m = mod) {
  a %= m;
  long long res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}
ll modinv(ll n) { return binpow(n, mod - 2, mod); }
vector<ll> ar[100001];
ll dp[100001] = {0};
ll dfs(ll u, ll p) {
  dp[u] = 1ll;
  for (auto it : ar[u]) {
    if (it != p) {
      dfs(it, u);
      dp[u] *= (dp[it] + 1ll);
      dp[u] %= mod;
    }
  }
}
ll ans[100001];
ll dfs2(ll u, ll p) {
  vector<ll> g;
  ll f = 1ll;
  vector<ll> k;
  for (auto it = ar[u].begin(); it != ar[u].end(); ++it) {
    if (*it != p) {
      k.push_back(*it);
      f *= (dp[(*it)] + 1ll);
      f %= mod;
      g.push_back(f);
    }
  }
  deque<ll> r;
  f = 1ll;
  for (auto it = ar[u].rbegin(); it != ar[u].rend(); ++it) {
    if (*it != p) {
      f *= (dp[(*it)] + 1ll);
      f %= mod;
      r.push_front(f);
    }
  }
  for (int x = 0; x < k.size(); x++) {
    ll t = 1;
    if (x > 0)
      t *= g[x - 1];
    t %= mod;
    if (x < k.size() - 1ll)
      t *= r[x + 1];
    t %= mod;
    ans[k[x]] *= (ans[u] * t + 1ll) % mod;
    ans[k[x]] %= mod;
    dfs2(k[x], u);
  }
  ans[u] *= dp[u];
  ans[u] %= mod;
}
void solve() {
  ll n;
  cin >> n >> mod;
  ll u, v;
  for (int x = 1; x <= n; x++)
    ans[x] = 1;
  for (int x = 0; x < n - 1; x++) {
    cin >> u >> v;
    ar[u].pb(v);
    ar[v].pb(u);
  }
  dfs(1, -1);
  dfs2(1, -1);
  for (int x = 1; x <= n; x++) {
    cout << ans[x] << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll i = 1;
  // cin>>i;
  ll k = 0;
  while (k < i) {
    solve();
    ++k;
  }
}