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

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned long long;
#define repi(n) for (long long i = 0; i < (n); ++i)
#define repj(n) for (long long j = 0; j < (n); ++j)
#define repk(n) for (long long k = 0; k < (n); ++k)
#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define itr(itr) for (auto &&v : (itr))
#define updatemax(t, v) (t = std::max(t, (v)))
#define updatemin(t, v) (t = std::min(t, (v)))

#include <boost/operators.hpp>

class modint : private boost::operators<modint>,
               private boost::equivalent<modint> {
  modint(const long long x, const bool y) : x(x) {}

public:
  static long long M;
  long long x;
  modint(const long long x = 0) : x((x % M + M) % M) {}
  modint operator-() const { return modint(x ? M - x : 0, true); }
  modint &operator+=(const modint &b) {
    return ((x += b.x) >= M ? x -= M : 0), *this;
  }
  modint &operator++() { return (++x >= M ? x -= M : 0), *this; }
  modint &operator-=(const modint &b) {
    return ((x -= b.x) < 0 ? x += M : 0), *this;
  }
  modint &operator--() { return (--x < 0 ? x += M : 0), *this; }
  modint &operator*=(const modint &b) { return x = x * b.x % M, *this; }
  modint &operator/=(const modint &b) { return *this *= b.pow(-1); }
  modint pow(long long b) const {
    long long r = 1, a = x;
    b = (b % (M - 1) + (M - 1)) % (M - 1);
    while (b) {
      if (b & 1)
        r = r * a % M;
      b = b >> 1;
      a = a * a % M;
    }
    return modint(r, true);
  }
  friend bool operator<(const modint &a, const modint &b) { return a.x < b.x; }
  friend std::istream &operator>>(std::istream &s, modint &a) {
    return s >> a.x, a = a.x, s;
  }
  friend std::ostream &operator<<(std::ostream &s, const modint &a) {
    return s << a.x;
  }
  static modint fact(const long long n) {
    if (n < 200100 && __facts[n] != 0)
      return __facts[n];
    long long res = 1;
    __facts[0] = modint(1, true);
    for (long long i = 1; i < 200100; i++) {
      res = res * i % M;
      __facts[i] = modint(res, true);
    }
    return __facts[n];
  }
  static modint __facts[200100];
};
long long modint::M = 1e9 + 7;
modint modint::__facts[200100];

int n;
modint fact_of_n_minus_1 = modint::fact(n - 1);
using pii = pair<int, int>;
vector<int> g[200100];
map<pii, pair<modint, int>> pm;
pair<modint, int> calcpm(int p, int parent) {
  vector<pair<modint, int>> childpms;
  itr(g[p]) {
    if (v == parent)
      continue;
    childpms.push_back(calcpm(v, p));
  }
  modint pmr(1);
  int size = 1;
  itr(childpms) {
    size += v.second;
    pmr *= v.first / modint::fact(v.second);
  }
  pmr *= modint::fact(size - 1);
  return pm[pii(parent, p)] = pair<modint, int>(pmr, size);
}
void calcpmrev(int p, int parent, modint usp, int uss) {
  modint whole = usp / modint::fact(uss);
  itr(g[p]) {
    if (v == parent)
      continue;
    auto [pp, ps] = pm[pii(p, v)];
    whole *= pp / modint::fact(ps);
  }
  itr(g[p]) {
    if (v == parent)
      continue;
    auto [part, partsize] = pm[pii(p, v)];
    modint ppp = whole / (part / modint::fact(partsize)) *
                 modint::fact(n - partsize - 1);
    pm[pii(v, p)] = pair<modint, int>(ppp, n - partsize);
    calcpmrev(v, p, ppp, n - partsize);
  }
}
int main() {
  cin >> n;
  repi(n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  int p;
  repi(n) {
    if (g[i].size() == 1) {
      p = i;
      break;
    }
  }
  calcpm(p, -1);
  calcpmrev(p, -1, 1, 0);
  repi(n) {
    modint re(1);
    int size = 0;
    itr(g[i]) {
      auto [pp, ps] = pm[pii(i, v)];
      re *= pp / modint::fact(ps);
      size += ps;
    }
    re *= modint::fact(size);
    cout << re << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned long long;
#define repi(n) for (long long i = 0; i < (n); ++i)
#define repj(n) for (long long j = 0; j < (n); ++j)
#define repk(n) for (long long k = 0; k < (n); ++k)
#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define itr(itr) for (auto &&v : (itr))
#define updatemax(t, v) (t = std::max(t, (v)))
#define updatemin(t, v) (t = std::min(t, (v)))

#include <boost/operators.hpp>

class modint : private boost::operators<modint>,
               private boost::equivalent<modint> {
  modint(const long long x, const bool y) : x(x) {}

public:
  static long long M;
  long long x;
  modint(const long long x = 0) : x((x % M + M) % M) {}
  modint operator-() const { return modint(x ? M - x : 0, true); }
  modint &operator+=(const modint &b) {
    return ((x += b.x) >= M ? x -= M : 0), *this;
  }
  modint &operator++() { return (++x >= M ? x -= M : 0), *this; }
  modint &operator-=(const modint &b) {
    return ((x -= b.x) < 0 ? x += M : 0), *this;
  }
  modint &operator--() { return (--x < 0 ? x += M : 0), *this; }
  modint &operator*=(const modint &b) { return x = x * b.x % M, *this; }
  modint &operator/=(const modint &b) { return *this *= b.pow(-1); }
  modint pow(long long b) const {
    long long r = 1, a = x;
    b = (b % (M - 1) + (M - 1)) % (M - 1);
    while (b) {
      if (b & 1)
        r = r * a % M;
      b = b >> 1;
      a = a * a % M;
    }
    return modint(r, true);
  }
  friend bool operator<(const modint &a, const modint &b) { return a.x < b.x; }
  friend std::istream &operator>>(std::istream &s, modint &a) {
    return s >> a.x, a = a.x, s;
  }
  friend std::ostream &operator<<(std::ostream &s, const modint &a) {
    return s << a.x;
  }
  static modint fact(const long long n) {
    if (n < 200100 && __facts[n] != 0)
      return __facts[n];
    long long r = 1;
    for (long long i = 1; i <= n; i++)
      r = r * i % M;
    return __facts[n] = modint(r, true);
  }
  static modint __facts[200100];
};
long long modint::M = 1e9 + 7;
modint modint::__facts[200100];

int n;
modint fact_of_n_minus_1 = modint::fact(n - 1);
using pii = pair<int, int>;
vector<int> g[200100];
map<pii, pair<modint, int>> pm;
pair<modint, int> calcpm(int p, int parent) {
  vector<pair<modint, int>> childpms;
  itr(g[p]) {
    if (v == parent)
      continue;
    childpms.push_back(calcpm(v, p));
  }
  modint pmr(1);
  int size = 1;
  itr(childpms) {
    size += v.second;
    pmr *= v.first / modint::fact(v.second);
  }
  pmr *= modint::fact(size - 1);
  return pm[pii(parent, p)] = pair<modint, int>(pmr, size);
}
void calcpmrev(int p, int parent, modint usp, int uss) {
  modint whole = usp / modint::fact(uss);
  itr(g[p]) {
    if (v == parent)
      continue;
    auto [pp, ps] = pm[pii(p, v)];
    whole *= pp / modint::fact(ps);
  }
  itr(g[p]) {
    if (v == parent)
      continue;
    auto [part, partsize] = pm[pii(p, v)];
    modint ppp = whole / (part / modint::fact(partsize)) *
                 modint::fact(n - partsize - 1);
    pm[pii(v, p)] = pair<modint, int>(ppp, n - partsize);
    calcpmrev(v, p, ppp, n - partsize);
  }
}
int main() {
  cin >> n;
  repi(n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  int p;
  repi(n) {
    if (g[i].size() == 1) {
      p = i;
      break;
    }
  }
  calcpm(p, -1);
  calcpmrev(p, -1, 1, 0);
  repi(n) {
    modint re(1);
    int size = 0;
    itr(g[i]) {
      auto [pp, ps] = pm[pii(i, v)];
      re *= pp / modint::fact(ps);
      size += ps;
    }
    re *= modint::fact(size);
    cout << re << endl;
  }
}