Problem:
Score : 600 points

Problem Statement

We have a tree with N vertices numbered 1 to N. The i-th edge in this tree connects Vertex a_i and b_i.
For each k=1, ..., N, solve the problem below:

Consider writing a number on each vertex in the tree in the following manner:

First, write 1 on Vertex k.

Then, for each of the numbers 2, ..., N in this order, write the number on the vertex chosen as follows:

Choose a vertex that still does not have a number written on it and is adjacent to a vertex with a number already written on it. If there are multiple such vertices, choose one of them at random.

Find the number of ways in which we can write the numbers on the vertices, modulo (10^9+7).

Constraints

2 \leq N \leq 2 \times 10^5

1 \leq a_i,b_i \leq N

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
:
a_{N-1} b_{N-1}

Output

For each k=1, 2, ..., N in this order, print a line containing the answer to the problem.

Sample Input 1

3
1 2
1 3

Sample Output 1

2
1
1

The graph in this input is as follows:

For k=1, there are two ways in which we can write the numbers on the vertices, as follows:

Writing 1, 2, 3 on Vertex 1, 2, 3, respectively

Writing 1, 3, 2 on Vertex 1, 2, 3, respectively

Sample Input 2

2
1 2

Sample Output 2

1
1

The graph in this input is as follows:

Sample Input 3

5
1 2
2 3
3 4
3 5

Sample Output 3

2
8
12
3
3

The graph in this input is as follows:

Sample Input 4

8
1 2
2 3
3 4
3 5
3 6
6 7
6 8

Sample Output 4

40
280
840
120
120
504
72
72

The graph in this input is as follows:

Solution in C (GCC 5.4.1) (judge verdict: Accepted)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h> // uint64_t

#define BUF_SIZE 30

// size: specify sizeof(str)
int get_str(char *str, int size) {
    if(!fgets(str, size, stdin)) return -1;
    return 0;
}

int get_int(void) {
  int num;
#ifdef BUF_SIZE
  char line[BUF_SIZE];
  if(!fgets(line, BUF_SIZE, stdin)) return 0;
  sscanf(line, "%d", &num);
#else
#error
#endif
  return num;
}

int get_int2(int *a1, int *a2) {
#ifdef BUF_SIZE
  char line[BUF_SIZE];
  if(!fgets(line, BUF_SIZE, stdin)) return -1;
  sscanf(line, "%d %d", a1, a2);
#else
#error
#endif
  return 0;
}

#define NUM_MAX 200000

struct list {
    int dst;
    struct list *next;
};

#define LPOOL_MAX (NUM_MAX*3+10)
static struct list lpool[LPOOL_MAX];
static int lpos = 0;
#define DIVISOR 1000000007

struct list *create_graph(int num) {
    struct list *graph = &lpool[lpos];
    lpos += num;
    int i;
    for(i = 0; i < num; i++) {
        graph[i].next = &graph[i];
    }
    return graph;
}

void add_list(struct list *head, int v) {
    struct list *elem = &lpool[lpos++];
    elem->dst = v;
    elem->next = head->next;
    head->next = elem;
}

static uint64_t factorial[NUM_MAX+1];
static uint64_t inv_factorial[NUM_MAX+1];

struct pair {
    int x;
    int y;
};

struct pair gcdext(int a, int b) {
    if(a == 0) { return (struct pair){0, 1}; }
    struct pair p = gcdext(b%a, a);
    return (struct pair){p.y - (b/a)*p.x, p.x};
}

void prep_factorial(int num) {
    factorial[0] = factorial[1] = 1;
    inv_factorial[0] = inv_factorial[1] = 1;
    int i;
    for(i = 2; i <= num; i++) {
        factorial[i] = (factorial[i-1]*i)%DIVISOR;
        struct pair p = gcdext(i, DIVISOR);
        int inv = (p.x + DIVISOR)%DIVISOR;
        inv_factorial[i] = (inv_factorial[i-1]*inv)%DIVISOR;
    }
}

uint64_t calc_combi(int num, int p) {
    uint64_t res = 1;
    res = (res * factorial[num])%DIVISOR;
    res = (res * inv_factorial[num-p])%DIVISOR;
    res = (res * inv_factorial[p])%DIVISOR;
    return res;
}


struct info {
    uint64_t res;
    int cnt;
};

struct info ans[NUM_MAX];

void dfs0(struct list *graph, int this, int parent) {
    struct list *head = &graph[this];
    struct list *elem;
    int cnt = 1; // myself
    for(elem = head->next; elem != head; elem = elem->next) {
        int dst = elem->dst;
        if(dst == parent) continue;
        dfs0(graph, dst, this);
        cnt += ans[dst].cnt;
    }
    uint64_t res = 1;
    int rem = cnt - 1;
    for(elem = head->next; elem != head; elem = elem->next) {
        int dst = elem->dst;
        if(dst == parent) continue;
        res = (res * calc_combi(rem, ans[dst].cnt))%DIVISOR;
        res = (res * ans[dst].res)%DIVISOR;
        rem = rem - ans[dst].cnt;
    }
    ans[this] = (struct info){res, cnt};
    return;
}

// uint64_t res = (ans[parent].res / (combi(ans[parent].cnt-1, ans[this].cnt)*ans[this].res))%DIVISOR;
struct info get_exclude_info(uint64_t whole, struct info *exclude, int vs) {
    uint64_t denom = (calc_combi(vs-1, exclude->cnt)*exclude->res)%DIVISOR;
    struct pair p = gcdext(denom, DIVISOR);
    int inv = (p.x + DIVISOR)%DIVISOR;
    uint64_t res = (whole * inv)%DIVISOR;
    return (struct info){res, vs - exclude->cnt};
}

void dfs_reroot(struct list *graph, int vs, int this, int parent) {
    if(parent != -1) {
        struct info info = get_exclude_info(ans[parent].res, &ans[this], vs);
        // update ans[this]
        ans[this].res = (ans[this].res * calc_combi(vs-1, info.cnt))%DIVISOR;
        ans[this].res = (ans[this].res * info.res)%DIVISOR;
        ans[this].cnt = vs;
    }
    struct list *head = &graph[this];
    struct list *elem;
    for(elem = head->next; elem != head; elem = elem->next) {
        int dst = elem->dst;
        if(dst == parent) continue;
        dfs_reroot(graph, vs, dst, this);
    }
    return;
}

int main(void) {
    int vs = get_int();
    int es = vs-1;
    int i;
    struct list *graph = create_graph(vs);
    prep_factorial(es);

    for(i = 0; i < es; i++) {
        int v1, v2;
        get_int2(&v1, &v2);
        // 1-indexed -> 0-indexed
        v1--; v2--;
        add_list(&graph[v1], v2);
        add_list(&graph[v2], v1);
    }
    dfs0(graph, 0, -1);
#ifdef DEBUG
    for(i = 0; i < vs; i++) {
        printf("%d: %llu(cnt: %d)\n", i, ans[i].res, ans[i].cnt);
    }
#endif
    dfs_reroot(graph, vs, 0, -1);
    for(i = 0; i < vs; i++) {
        printf("%d\n", (int)ans[i].res);
    }
    return 0;
}