Problem p02728 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using Int = long long;
using namespace std;

#define ARC(i, a, b) for (Int i = (a); i < (b); ++i)
#define RC(i, n) ARC(i, 0, n)
#define ARCH(i, a, b) for (Int i = (a)-1; i >= (b); --i)
#define RCH(i, n) ARCH(i, n, 0)
template <class T> T ant(const T &a, const T &b) { return a > b ? a : b; };
template <class T> T ont(const T &a, const T &b) { return a < b ? a : b; };
template <class T> void th(T &a, const T &b, T (*f)(const T &, const T &)) {
  a = f(a, b);
}
#define anth(a, b) th(a, b, ant)
#define onth(a, b) th(a, b, ont)

template <class T, int MOD> class Modint {
  T val;

public:
  Modint(T val = 0) : val(val) {}
  operator T() { return val; }
  Modint &operator+=(const Modint &a) {
    val += a.val;
    if (val >= MOD)
      val -= MOD;
    return *this;
  }
  Modint &operator-=(const Modint &a) {
    val -= a.val;
    if (val < 0)
      val += MOD;
    return *this;
  }
  Modint &operator*=(const Modint &a) {
    val = val * a.val % MOD;
    return *this;
  }
  void inverse() {
    T x[2] = {MOD, val}, a[2] = {0, 1};
    int i;
    for (i = 0; x[!i]; i ^= 1) {
      a[i] -= x[i] / x[!i] * a[!i];
      x[i] = x[i] % x[!i];
    }
    if (!i)
      a[i] += MOD;
    val = a[i];
  }
  Modint &operator/=(Modint a) {
    a.inverse();
    return *this *= a;
  }
  friend Modint modpow(Modint a, int n) {
    Modint ret(1);
    while (n) {
      if (n & 1)
        ret *= a;
      a *= a;
      n >>= 1;
    }
    return ret;
  }
  friend Modint operator+(Modint a, const Modint &b) { return a += b; }
  friend Modint operator-(Modint a, const Modint &b) { return a -= b; }
  friend Modint operator*(Modint a, const Modint &b) { return a *= b; }
  friend Modint operator/(Modint a, const Modint &b) { return a /= b; }
  friend std::ostream &operator<<(std::ostream &os, const Modint &a) {
    return os << a.val;
  }
};

using Mint = Modint<long long, 1000000007>;

Mint fact[212345] = {1};

struct edge {
  int from, to;
  int count;
  edge *rev;
  edge(int from, int to) : from(from), to(to), count(0) {}
};

int n;
std::vector<std::vector<edge>> e;
std::vector<Mint> ans;

void count(edge &i) {
  for (edge &j : e[i.to]) {
    if (j.to == i.from) {
      i.rev = &j;
    } else {
      count(j);
      i.count += j.count;
    }
  }
  ++i.count;
  i.rev->count = n - i.count;
}

void down(int from, int prev) {
  int sum = 0;
  for (edge &i : e[from])
    if (i.to != prev) {
      down(i.to, from);
      ans[from] *= ans[i.to] / fact[i.count];
      sum += i.count;
    }
  ans[from] *= fact[sum];
}

void up(edge &i) {
  ans[i.to] = ans[i.from] * fact[i.count] * fact[n - i.count - 1] /
              (fact[i.count - 1] * fact[n - i.count]);
  for (edge &j : e[i.to])
    if (j.to != i.from) {
      up(j);
    }
}

int main() {
  cin >> n;
  e.resize(n);
  ans.resize(n, 1);
  ARC(i, 1, n) { fact[i] = fact[i - 1] * Mint(i); }
  RC(i, n - 1) {
    int ai, bi;
    cin >> ai >> bi;
    --ai;
    --bi;
    e[ai].push_back(edge(ai, bi));
    e[bi].push_back(edge(bi, ai));
  }
  for (edge &i : e[0])
    count(i);
  down(0, -1);
  for (edge &i : e[0])
    up(i);

  /*
  RC(i, n){
          for(edge &j : e[i]){
                  cout << j.from << "->" << j.to << ": " << j.count << endl;
          }
  }
  */

  RC(i, n) { cout << ans[i] << endl; }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

template <int MOD> class Modint {
public:
  long long val;
  Modint(long long val = 0) : val(val) {}
  Modint &operator+=(const Modint &a) {
    val += a.val;
    if (val >= MOD)
      val -= MOD;
    return *this;
  }
  Modint &operator-=(const Modint &a) {
    val -= a.val;
    if (val < 0)
      val += MOD;
    return *this;
  }
  Modint &operator*=(const Modint &a) {
    val = val * a.val % MOD;
    return *this;
  }
  void inverse() {
    long long x[2] = {MOD, val};
    long long a[2] = {1, 0};
    int i;
    for (i = 0; x[!i]; i ^= 1) {
      a[!i] -= x[i] / x[!i] * a[i];
      x[i] = x[i] % x[!i];
    }
    if (!i)
      a[!i] += MOD;
    val = a[!i];
  }
  Modint &operator/=(Modint a) {
    a.inverse();
    return *this *= a;
  }
  friend Modint modpow(Modint a, long long n) {
    Modint ret(1);
    while (n) {
      if (n & 1)
        ret *= a;
      a *= a;
      n >>= 1;
    }
    return ret;
  }
  friend Modint operator+(Modint a, const Modint &b) { return a += b; }
  friend Modint operator-(Modint a, const Modint &b) { return a -= b; }
  friend Modint operator*(Modint a, const Modint &b) { return a *= b; }
  friend Modint operator/(Modint a, const Modint &b) { return a /= b; }
  friend std::istream &operator>>(std::istream &is, Modint &a) {
    return is >> a.val;
  }
  friend std::ostream &operator<<(std::ostream &os, const Modint &a) {
    return os << a.val;
  }
};

using Mint = Modint<1000000007>;
using Int = long long;
using namespace std;

#define ARC(i, a, b) for (Int i = (a); i < (b); ++i)
#define RC(i, n) ARC(i, 0, n)
#define ARCH(i, a, b) for (Int i = (a)-1; i >= (b); --i)
#define RCH(i, n) ARCH(i, n, 0)
template <class T> T ant(const T &a, const T &b) { return a > b ? a : b; };
template <class T> T ont(const T &a, const T &b) { return a < b ? a : b; };
template <class T> void th(T &a, const T &b, T (*f)(const T &, const T &)) {
  a = f(a, b);
}
#define anth(a, b) th(a, b, ant)
#define onth(a, b) th(a, b, ont)

Int n;

Mint fact[212345];

struct edge {
  Int from, to;
  Int count;
  bool memo;
  Mint ans;
  edge(Int from, Int to) : from(from), to(to), count(-1), memo(false), ans(0) {}
};

vector<edge> a[212345];

Int count(edge &e) {
  if (e.count == -1) {
    Int cnt = 0;
    for (edge &i : a[e.to]) {
      if (i.to != e.from)
        cnt += count(i);
    }
    e.count = cnt + 1;
  }
  return e.count;
}

Mint ans(edge &e) {
  if (!e.memo) {
    Mint ret = fact[count(e) - 1];
    for (edge &i : a[e.to]) {
      if (i.to != e.from) {
        ret *= ans(i);
        ret /= fact[count(i)];
      }
    }
    // cout << e.from << " " << e.to << " " << ret << endl;
    e.memo = true;
    e.ans = ret;
  }
  return e.ans;
}

int main() {
  cin >> n;

  fact[0] = 1;
  ARC(i, 1, n) { fact[i] = fact[i - 1] * Mint(i); }

  RC(i, n - 1) {
    Int at, bt;
    cin >> at >> bt;
    --at;
    --bt;
    a[at].push_back(edge(at, bt));
    a[bt].push_back(edge(bt, at));
  }

  RC(i, n) {
    Mint at = fact[n - 1];

    for (edge &j : a[i]) {
      at *= ans(j);
      at /= fact[count(j)];
    }

    cout << at << endl;
  }
}