Language: C++

#include <cstdio>
#include <functional>
#include <vector>
#include <tuple>

using namespace std;

using ll = long long;
ll mod;
ll add(ll l, ll r) { return (((l)+(r))%mod); }
ll mul(ll l, ll r) { return (((l)*(r))%mod); }
// function<ll (ll, ll)> get_mul(ll m) { return [=](ll l, ll r) { return (((l)*(r))%m); }; }

constexpr ll nearest_pow2(ll n)
{
  ll i = 1;
  while (i < n) i <<= 1;
  return i;
}

template<typename T, ll N_, T (*op)(T, T), T id, ll N = nearest_pow2(N_)>
struct Segtree {
  T nodes[N-1+N];
  int index;
  int size;
  int offset;

  void init()
  {
    index = 0;
  }

  int add(T elem)
  {
    nodes[N-1+index] = elem;
    return index++;
  }

  void build()
  {
    size = nearest_pow2(index);
    for (; index < size; index++) nodes[N-1+index] = id;
    offset = N-1-(size-1);
    build1(offset);
  }

  T build1(int i)
  {
    if ((N-1 <= i) && (i < N-1+size)) return nodes[i];
    int r = ((i-offset)+1)<<1;
    int l = r-1;
    T p = op(build1(l+offset), build1(r+offset));
    nodes[i] = p;
    return p;
  }

  T query(int left, int right)
  {
    if (left == right) return id;
    if (right <= 0 || size <= left) return id;
    return query1(left, right, 0, 0, size);
  }

  T query1(int left, int right, int cur, int curl, int curr)
  {
    if (left == curl && right == curr) return nodes[cur+offset];
    int rcur = (cur+1)<<1;
    int lcur = rcur-1;
    int lcurl, lcurr, rcurl, rcurr;
    lcurl = curl;
    lcurr = lcurl+((curr-curl)>>1);
    rcurl = lcurr;
    rcurr = rcurl+((curr-curl)>>1);
    if (lcurl <= left && right <= lcurr)
      return query1(left, right, lcur, lcurl, lcurr);
    if (rcurl <= left && right <= rcurr)
      return query1(left, right, rcur, rcurl, rcurr);
    return op(query1(left, lcurr, lcur, lcurl, lcurr),
              query1(rcurl, right, rcur, rcurl, rcurr));
  }
};

vector<tuple<int, ll>> G[100000];
ll ans[100000];

ll dfs(int cur, int last)
{
  ll combinations = 1;
  for (auto& nc : G[cur]) {
    int n = get<0>(nc);
    if (n == last) continue;
    ll c = dfs(n, cur);
    get<1>(nc) = c;
    combinations = mul(combinations, c+1);
  }
  return combinations;
}

void dfs2(int cur, int last, ll lcc)
{
  static Segtree<ll, 100000, &mul, 1> tree;
  ll *indices;
  indices = malloc(sizeof(ll) * G[cur].size());

  tree.init();
  for (auto& nc : G[cur]) {
    int n = get<0>(nc);
    ll c;
    if (n == last) {
      c = lcc;
    }
    else {
      c = get<1>(nc);
    }
    indices[n] = tree.add(c+1);
  }
  tree.build();
  ll *prods;
  prods = malloc(sizeof(ll) * G[cur].size());

  for (auto& nc : G[cur]) {
    int n = get<0>(nc);
    if (n == last) continue;
    ll cnc = mul(tree.query(0, indices[n]), tree.query(indices[n]+1, G[cur].size()));
    prods[indices[n]] = cnc;
  }
  ans[cur] = tree.query(0, G[cur].size());
  for (auto& nc : G[cur]) {
    int n = get<0>(nc);
    if (n == last) continue;
    ll cnc = prods[indices[n]];
    dfs2(n, cur, cnc);
  }
}

int main()
{
  int n;
  ll m;
  scanf("%d %lld", &n, &m);
  mod = m;
  for (int i = 0; i < n-1; i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    u--;
    v--;
    G[u].push_back(make_tuple(v, 1));
    G[v].push_back(make_tuple(u, 1));
  }
  dfs(0, -1);
  dfs2(0, -1, 0);
  for (int i = 0; i < n; i++) {
    printf("%lld\n", ans[i]);
  }
  return 0;
}


Compiler output:
a.cc: In function 'void dfs2(int, int, ll)':
a.cc:103:13: error: 'malloc' was not declared in this scope
  103 |   indices = malloc(sizeof(ll) * G[cur].size());
      |             ^~~~~~
a.cc:3:1: note: 'malloc' is defined in header '<cstdlib>'; this is probably fixable by adding '#include <cstdlib>'
    2 | #include <functional>
  +++ |+#include <cstdlib>
    3 | #include <vector>