Language: C++

#include <algorithm>
#include <cmath>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <unordered_map>
#include <vector>

using namespace std;

#define ll long long
#define el '\n'
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repi(i, a, b) for (int i = a; i >= b; i--)
#define MODULO 1000000007
#define VI vector<int>
#define VII vector<pair<int, int>>
#define PII pair<int, int>
#define MP make_pair
#define MAXN (int)2e5
// #define log(fmt, ...) printf(fmt, ##__VA_ARGS__)
#define log(fmt, ...)

int extgcd(int a, int b, int &x, int &y) {
    if (a == 0) {
        x = 0;
        y = 1;
        return b;
    }
    int x1, y1;
    int d = extgcd(b % a, a, x1, y1);
    x = y1 - (b / a) * x1;
    y = x1;
    return d;
}

int invmod(int a, int m) {
    int x, y;
    int g = extgcd(a, m, x, y);
    if (g != 1) {
        return -1;
    } else {
        x = (x % m + m) % m;
        return x;
    }
}

typedef struct {
    int to;
    int edge_no;
} edge_info;

int n;
vector<edge_info> edge[MAXN];
int fac[MAXN + 1], fnv[MAXN + 1];
int ans[MAXN];

typedef struct {
    int depth;
    int parent;
    int edge_no;
} node_info;

typedef struct {
    int cnt;
    int all_nodes;  // subtree node count, including root
} updown_info;

namespace my {

int mul(int a, int b) {
    return (ll)a * b % MODULO;
}

int div(int a, int b) {
    return (ll)a * invmod(b, MODULO) % MODULO;
}

// a * k!
int mulfac(int a, int k) {
    return (ll)a * fac[k] % MODULO;
}

// a / k!
int divfac(int a, int k) {
    return (ll)a * fnv[k] % MODULO;
}

}  // namespace my

void solve() {
    queue<int> q;
    q.push(0);
    node_info info[n];
    rep(i, 0, n) { info[i].parent = -1; }
    info[0] = {0, 0, -1};
    while (!q.empty()) {
        int h = q.front();
        q.pop();
        for (auto x : edge[h]) {
            if (info[x.to].parent == -1) {
                q.push(x.to);
                info[x.to] = {info[h].depth + 1, h, x.edge_no};
            }
        }
    }

    int order[n];
    rep(i, 0, n) { order[i] = i; }
    auto cmp = [&info](auto i, auto j) {
        return info[i].depth > info[j].depth;
    };
    // todo: try begin(order) and end(order)
    sort(order, order + n, cmp);

    updown_info up[n];
    // up, from leaf to root
    for (auto node : order) {
        // cout << "depth(" << node << ")=" << info[node].depth << el;
        int from = info[node].parent;
        ll cnt = 1;
        int all_nodes = 1;  // children count
        for (auto x : edge[node]) {
            if (x.to == from) {
                continue;
            }
            updown_info child = up[x.to];
            // cout << node << "->" << x.to << " cnt: " << child.cnt << ' '
            //      << "all nodes: " << child.all_nodes << el;

            cnt = my::mul(cnt, child.cnt);
            cnt = my::divfac(cnt, child.all_nodes);

            // cout << x.to << " fac" << fac[child.all_nodes] << el;
            // cout << x.to << " fnv" << fnv[child.all_nodes] << el;
            all_nodes += child.all_nodes;
        }
        cnt = my::mulfac(cnt, all_nodes - 1);
        up[node] = {(int)cnt, all_nodes};
        log("node=%d,cnt=%lld,all_nodes=%d\n", node, cnt, all_nodes);
    }

    // down, from root to leaf
    reverse(order, order + n);
    updown_info down[n];  // actually the answer
    for (auto node : order) {
        int parent = info[node].parent;
        int ans1 = up[node].cnt;
        log("before ans1(%d)=%d\n", node, ans1);

        if (parent != node) {
            int cnt_from_parent = (ll)down[parent].cnt;
            // down[parent] is the answer of parent as root
            cnt_from_parent = my::divfac(cnt_from_parent, n - 1);                       
            assert(cnt_from_parent>0);
            cnt_from_parent = my::mulfac(cnt_from_parent, up[node].all_nodes);          
            assert(cnt_from_parent>0);
            cnt_from_parent = my::mulfac(cnt_from_parent, n - 1 - up[node].all_nodes);  
            assert(cnt_from_parent>0);
            cnt_from_parent = my::div(cnt_from_parent, up[node].cnt);                
            assert(cnt_from_parent>0);
            log("cnt_from_parent(%d,%d)=%d\n", node + 1, parent + 1, cnt_from_parent);

            int all_nodes_from_parent = n - up[node].all_nodes;
            log("all_nodes_from_parent(%d,%d)=%d\n", node + 1, parent + 1, all_nodes_from_parent);

            ans1 = my::divfac(ans1, up[node].all_nodes - 1);
            log("all_nodes(%d)=%d\n", node + 1, up[node].all_nodes);
            assert(ans1 > 0);
            ans1 = my::mulfac(ans1, n - 1);
            assert(ans1 > 0);
            ans1 = my::mul(ans1, cnt_from_parent);
            assert(ans1 > 0);
            ans1 = my::divfac(ans1, all_nodes_from_parent);
            log("ans1(%d)=%d\n", node, ans1);
        }
        down[node] = {(int)ans1, n};
        ans[node] = ans1;
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> n;
    rep(i, 0, n - 1) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        edge[a].push_back({b, i});
        edge[b].push_back({a, i + n});
    }

    fac[0] = 1;
    fac[1] = 1;
    fnv[1] = 1;
    fnv[0] = 1;
    rep(i, 2, n + 1) {
        fac[i] = (ll)fac[i - 1] * i % MODULO;
        fnv[i] = invmod(fac[i], MODULO);
    }

    solve();

    rep(k, 0, n) { cout << ans[k] << el; }
}


Compiler output:
a.cc: In function 'void solve()':
a.cc:156:13: error: 'assert' was not declared in this scope
  156 |             assert(cnt_from_parent>0);
      |             ^~~~~~
a.cc:8:1: note: 'assert' is defined in header '<cassert>'; this is probably fixable by adding '#include <cassert>'
    7 | #include <unordered_map>
  +++ |+#include <cassert>
    8 | #include <vector>