Language: C++

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

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 PII pair<int, int>
#define MP make_pair

void bfs(int n, vector<int> edge[], int depth[], int parent[]) {
    queue<int> q;
    q.push(0);
    fill_n(depth, n, -1);
    fill_n(parent, n, -1);
    depth[0] = 0;
    parent[0] = 0;
    while (!q.empty()) {
        int h = q.front();
        q.pop();
        for (auto v : edge[h]) {
            if (parent[v] == -1) {
                depth[v] = depth[h] + 1;
                parent[v] = h;
                q.push(v);
            }
        }
    }
}
int dfs(int from, int node, int choices, vector<int> edge[], int depth[],
        int parent[]) {
    ll ans = 1;
    for (auto v : edge[node]) {
        if (v == from) {
            continue;
        }
        depth[v] = depth[node] + 1;
        parent[v] = node;
        ll ans1 =
            dfs(node, v, choices - 1 + edge[v].size() - 1, edge, depth, parent);
    }
    return ans % MODULO;
}

int dfs1(int from, int number, vector<int> edge[], vector<int> ch, bool used[],
         int n) {
    // todo: 如果ch里面没用的都是叶子，那么直接返回没有使用的个数的阶乘就行了。
    if (number > n) {
        return 1;
    }
    for (auto x : edge[from]) {
        if (!used[x]) {
            ch.push_back(x);
        }
    }
    // printf("number=%d,ch=", number);
    // for (auto x : ch) {
    //     cout << x << ',';
    // }
    // cout << el;
    ll ans = 0;
    for (auto x : ch) {
        if (!used[x]) {
            used[x] = true;
            int ans1 = dfs1(x, number + 1, edge, ch, used, n);
            ans = (ans + ans1) % MODULO;
            used[x] = false;
        }
    }
    for (auto x : edge[from]) {
        if (!used[x]) {
            ch.push_back(x);
        }
    }
    return ans;
}

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;
    }
}

map<PII, PII> dfs2_cache;

// first: ans count; second: children tree node count
PII dfs2(int from, int node, VI edge[], int n, int fac[], int fnv[]) {
    if (dfs2_cache.find(MP(from, node)) != dfs2_cache.end()) {
        return dfs2_cache[MP(from, node)];
    }
    // printf("from=%d,node=%d\n", from, node);
    ll ans = 1;
    int cc = 0;  // children count
    for (auto x : edge[node]) {
        if (x == from) {
            continue;
        }
        PII ans1 = dfs2(node, x, edge, n, fac, fnv);
        // cout << node << "->" << x << ' ' << ans1.first << ' ' << ans1.second << el;
        ans = ans * ans1.first % MODULO;
        ans = ans * fnv[ans1.second] % MODULO;
        // cout << fnv[ans1.second] << el;
        cc += ans1.second;
    }
    ans = ans * fac[cc] % MODULO;
    // printf("from=%d,node=%d,count=%lld,children+1=%d\n", from, node, ans,
        //    cc + 1);
    dfs2_cache[MP(from, node)] = MP(ans, cc+1);
    return MP(ans, cc + 1);
}

int solve(int k, int n, vector<int> edge[], int depth[], int parent[],
          int fac[], int fnv[]) {
    // fill_n(depth, n, -1);
    // fill_n(parent, n, -1);
    // depth[0] = 0;
    // parent[0] = 0;
    // return dfs(0, 0, edge[0].size(), edge, depth, parent);
    // vector<int> ch;  // choices;
    // ch.push_back(k);
    // bool used[n];
    // fill_n(used, n, false);
    // used[k] = true;
    // return dfs1(k, 2, edge, ch, used, n);
    return dfs2(k, k, edge, n, fac, fnv).first;
}

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

    // cout << (ll) invmod(97632, MODULO) * 97632 % MODULO << el;

    int n;
    cin >> n;
    vector<int> edge[n];
    rep(i, 0, n - 1) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        edge[a].push_back(b);
        edge[b].push_back(a);
    }
    int depth[n];
    int parent[n];
    // bfs(n, edge, depth, parent);

    int fac[n + 1], fnv[n + 1];
    fac[0] = 1;
    fac[1] = 1;
    fnv[1]=1;
    rep(i, 2, n + 1) {
        fac[i] = (ll)fac[i - 1] * i % MODULO;
        fnv[i] = invmod(fac[i], MODULO);
        assert((ll)fac[i] * fnv[i] % MODULO == 1);
    }
    rep(k, 0, n) {
        // cout << "---------" << el;
        cout << solve(k, n, edge, depth, parent, fac, fnv) << el;
        // printf("%d,depth=%d,parent=%d\n", k, depth[k], parent[k]);
    }
}


Compiler output:
a.cc: In function 'int main()':
a.cc:181:9: error: 'assert' was not declared in this scope
  181 |         assert((ll)fac[i] * fnv[i] % MODULO == 1);
      |         ^~~~~~
a.cc:7:1: note: 'assert' is defined in header '<cassert>'; this is probably fixable by adding '#include <cassert>'
    6 | #include <map>
  +++ |+#include <cassert>
    7 |