Problem p03181 (C++)

Buggy submission (status: Runtime Error):
//~ while (clock()<=69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
//~ #pragma GCC target ("avx2")
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC
//target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") ~ #pragma
//GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

#define shandom_ruffle random_shuffle

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
const int nax = 3007;
ll mod = 1000 * 1000 * 1000 + 7;
const ll inf = 1e18;

int n;

vi graf[nax];

ll dol[nax];
ll gor[nax];

ll wyn[nax];

void dfs1(int v, int oj) {
  for (int &i : graf[v]) {
    if (i == oj) {
      swap(i, graf[v].back());
      graf[v].pop_back();
      break;
    }
  }
  dol[v] = 1;
  for (int i : graf[v]) {
    dfs1(i, v);
    dol[v] = (dol[v] * dol[i]) % mod;
  }
  dol[v] = (dol[v] + 1) % mod;
}

ll pre[nax];
ll suf[nax];

void dfs2(int v, int oj) {
  int r = graf[v].size();
  pre[0] = suf[r + 1] = 1;
  for (int i = 1; i <= r; i++)
    pre[i] = (pre[i - 1] * dol[graf[v][i - 1]]) % mod;
  for (int i = r; i; i--)
    suf[i] = (suf[i + 1] * dol[graf[v][i - 1]]) % mod;
  ll mno = gor[v];
  //~ debug() << v;
  //~ debug() << range(pre+1, pre+1+r);
  //~ debug() << range(suf+1, suf+1+r);
  for (int i = 1; i <= r; i++) {
    ll x = (pre[i - 1] * suf[i + 1]) % mod;
    x = (x * mno + 1) % mod;
    gor[graf[v][i - 1]] = x;
  }
  wyn[v] = gor[v] % mod;
  for (int i : graf[v])
    wyn[v] = (wyn[v] * dol[i]) % mod;
  for (int i : graf[v]) {
    dfs2(i, v);
  }
}

int main() {
  scanf("%d%lld", &n, &mod);
  for (int i = 1; i < n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    graf[a].push_back(b);
    graf[b].push_back(a);
  }
  dfs1(1, 0);
  gor[1] = 1;
  dfs2(1, 0);

  for (int i = 1; i <= n; i++)
    printf("%lld\n", wyn[i]);
  return 0;
}


Fixed submission:
//~ while (clock()<=69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
//~ #pragma GCC target ("avx2")
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC
//target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") ~ #pragma
//GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

#define shandom_ruffle random_shuffle

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
const int nax = 1000 * 1007;
ll mod = 1000 * 1000 * 1000 + 7;
const ll inf = 1e18;

int n;

vi graf[nax];

ll dol[nax];
ll gor[nax];

ll wyn[nax];

void dfs1(int v, int oj) {
  for (int &i : graf[v]) {
    if (i == oj) {
      swap(i, graf[v].back());
      graf[v].pop_back();
      break;
    }
  }
  dol[v] = 1;
  for (int i : graf[v]) {
    dfs1(i, v);
    dol[v] = (dol[v] * dol[i]) % mod;
  }
  dol[v] = (dol[v] + 1) % mod;
}

ll pre[nax];
ll suf[nax];

void dfs2(int v, int oj) {
  int r = graf[v].size();
  pre[0] = suf[r + 1] = 1;
  for (int i = 1; i <= r; i++)
    pre[i] = (pre[i - 1] * dol[graf[v][i - 1]]) % mod;
  for (int i = r; i; i--)
    suf[i] = (suf[i + 1] * dol[graf[v][i - 1]]) % mod;
  ll mno = gor[v];
  //~ debug() << v;
  //~ debug() << range(pre+1, pre+1+r);
  //~ debug() << range(suf+1, suf+1+r);
  for (int i = 1; i <= r; i++) {
    ll x = (pre[i - 1] * suf[i + 1]) % mod;
    x = (x * mno + 1) % mod;
    gor[graf[v][i - 1]] = x;
  }
  wyn[v] = gor[v] % mod;
  for (int i : graf[v])
    wyn[v] = (wyn[v] * dol[i]) % mod;
  for (int i : graf[v]) {
    dfs2(i, v);
  }
}

int main() {
  scanf("%d%lld", &n, &mod);
  for (int i = 1; i < n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    graf[a].push_back(b);
    graf[b].push_back(a);
  }
  dfs1(1, 0);
  gor[1] = 1;
  dfs2(1, 0);

  for (int i = 1; i <= n; i++)
    printf("%lld\n", wyn[i]);
  return 0;
}