Language: C++


#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <numeric>

template <class T, class U>
inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

template <class T, class U>
inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

// [l, r) from l to r
struct range {
  struct itr {
    int i;
    constexpr itr(int i_): i(i_) { }
    constexpr void operator ++ () { ++i; }
    constexpr int operator * () const { return i; }
    constexpr bool operator != (itr x) const { return i != x.i; }
  };
  const itr l, r;
  constexpr range(int l_, int r_): l(l_), r(std::max(l_, r_)) { }
  constexpr itr begin() const { return l; }
  constexpr itr end() const { return r; }
};

// [l, r) from r to l
struct revrange {
  struct itr {
    int i;
    constexpr itr(int i_): i(i_) { }
    constexpr void operator ++ () { --i; }
    constexpr int operator * () const { return i; }
    constexpr bool operator != (itr x) const { return i != x.i; }
  };
  const itr l, r;
  constexpr revrange(int l_, int r_): l(l_ - 1), r(std::max(l_, r_) - 1) { }
  constexpr itr begin() const { return r; }
  constexpr itr end() const { return l; }
};

template <class T>
class modulo_int {
public:
  static constexpr int mod = T::value;
  static_assert(mod > 0, "mod must be positive");
private:
  long long value;
  constexpr void normalize() {
    value %= mod;
    if (value < 0) value += mod;
  }
public:
  constexpr modulo_int(long long value_ = 0): value(value_) { normalize(); }
  constexpr modulo_int operator - () const { return modulo_int(mod - value); }
  constexpr modulo_int operator ~ () const { return power(mod - 2); }
  constexpr long long operator () () const { return value; }
  constexpr modulo_int operator + (const modulo_int &rhs) const { return modulo_int(*this) += rhs; }
  constexpr modulo_int &operator += (const modulo_int &rhs) {
    if ((value += rhs.value) >= mod) value -= mod;
    return (*this);
  }
  constexpr modulo_int operator - (const modulo_int &rhs) const { return modulo_int(*this) -= rhs; }
  constexpr modulo_int &operator -= (const modulo_int &rhs) {
    if ((value += mod - rhs.value) >= mod) value -= mod;
    return (*this);
  }
  constexpr modulo_int operator * (const modulo_int &rhs) const { return modulo_int(*this) *= rhs; }
  constexpr modulo_int &operator *= (const modulo_int &rhs) {
    (value *= rhs.value) %= mod;
    return (*this);
  }
  constexpr modulo_int operator / (const modulo_int &rhs) const { return modulo_int(*this) /= rhs; }
  constexpr modulo_int &operator /= (const modulo_int &rhs) {
    return (*this) *= ~rhs;
  }
  constexpr bool operator == (const modulo_int &rhs) const {
    return value == rhs();
  }
  constexpr bool operator != (const modulo_int &rhs) const {
    return value != rhs();
  }
  constexpr modulo_int power (uint64_t exp) const {
    modulo_int result(1), mult(*this);
    while (exp > 0) {
      if (exp & 1) result *= mult;
      mult *= mult;
      exp >>= 1;
    }
    return result;
  }
  friend std::istream &operator >> (std::istream &stream, modulo_int &lhs) {
    stream >> lhs.value;
    lhs.normalize();
    return stream;
  }
  friend std::ostream &operator << (std::ostream &stream, const modulo_int &rhs) {
    return stream << rhs.value;
  }
};

template <class T, std::size_t N>
class factorials {
public:
  using value_type = T;
  static constexpr std::size_t size = N;

public:
  std::array<value_type, size + 1> fact{};
  std::array<value_type, size + 1> fact_inv{};

  factorials() {
    fact.front() = value_type(1);
    for (std::size_t i = 1; i <= size; ++i) {
      fact[i] = fact[i - 1] * value_type(i);
    }
    fact_inv.back() = ~fact.back();
    for (std::size_t i = size; i > 0; --i) {
      fact_inv[i - 1] = fact_inv[i] * value_type(i);
    }
  }

  value_type operator () (std::size_t n, std::size_t r) const {
    return fact[n] * fact_inv[n - r] * fact_inv[r];
  }

};

using modint = modulo_int<std::integral_constant<int, 1000000007>>;
factorials<modint, 200000> fact;

template <class T>
struct fix_point: private T {
  explicit constexpr fix_point(T &&func): T(std::forward<T>(func)) { }
  template <class... Args>
  constexpr decltype(auto) operator () (Args &&... args) const {
    return T::operator()(*this, std::forward<Args>(args)...);
  }
};

template <class T>
constexpr decltype(auto) make_fix_point(T &&func) {
  return fix_point<T>{std::forward<T>(func)};
}

int main() {
  int N;
  std::cin >> N;
  std::vector<std::vector<int>> graph(N);
  for (int i: range(1, N)) {
    int a, b;
    std::cin >> a >> b;
    --a; --b;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  std::vector<std::vector<modint>> dp(N);
  for (int i: range(0, N)) {
    dp[i].assign(graph[i].size(), 0);
  } 
  std::vector<int> count(N);
  make_fix_point([&](auto dfs, int u, int p) -> modint {
    modint res(1);
    for (int i: range(0, graph[u].size())) {
      int v = graph[u][i];
      if (v != p) {
        dp[u][i] = dfs(v, u);
        dp[u][i] *= fact.fact_inv[count[v]];
        count[u] += count[v];
        res *= dp[u][i];
      }
    }
    res *= fact.fact[count[u]];
    ++count[u];
    return res;
  })(0, -1);
  std::vector<modint> ans(N);
  make_fix_point([&](auto apply, int u, int p, modint up) -> void {
    ans[u] = fact.fact[N - 1];
    for (int i: range(0, graph[u].size())) {
      if (graph[u][i] == p) {
        dp[u][i] = up * fact.fact_inv[N - count[u]];
      }
      ans[u] *= dp[u][i];
    }
    std::vector<modint> pref(graph[u].size() + 1), suff(graph[u].size() + 1);
    pref.front() = suff.front() = 1;
    for (int i: range(0, graph[u].size())) {
      pref[i + 1] = pref[i] * dp[u][i];
      suff[i + 1] = suff[i] * dp[u][graph[u].size() - i - 1]; 
    }
    for (int i: range(0, graph[u].size())) {
      int v = graph[u][i];
      if (v != p) {
        apply(v, u, fact.fact[N - count[v] - 1] * pref[i] * suff[graph[u].size() - i - 1]);
      }
    }
  })(0, -1, 0);
  for (auto x: ans) {
    std::cout << x << '\n';
  }
  return 0;
}


Compiler output:
a.cc:97:31: error: 'uint64_t' has not been declared
   97 |   constexpr modulo_int power (uint64_t exp) const {
      |                               ^~~~~~~~
a.cc: In instantiation of 'class factorials<modulo_int<std::integral_constant<int, 1000000007> >, 200000>':
a.cc:144:28:   required from here
  144 | factorials<modint, 200000> fact;
      |                            ^~~~
a.cc:123:36: error: 'factorials<T, N>::fact' has incomplete type
  123 |   std::array<value_type, size + 1> fact{};
      |                                    ^~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:64,
                 from /usr/include/c++/14/string:51,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:2:
/usr/include/c++/14/bits/stl_pair.h:99:12: note: declaration of 'struct std::array<modulo_int<std::integral_constant<int, 1000000007> >, 200001>'
   99 |     struct array;
      |            ^~~~~
a.cc:124:36: error: 'factorials<T, N>::fact_inv' has incomplete type
  124 |   std::array<value_type, size + 1> fact_inv{};
      |                                    ^~~~~~~~
/usr/include/c++/14/bits/stl_pair.h:99:12: note: declaration of 'struct std::array<modulo_int<std::integral_constant<int, 1000000007> >, 200001>'
   99 |     struct array;
      |            ^~~~~