Tree Coloring
Practice
3.9 (11 votes)
Dynamic programming
Combinatorics
Tree
Medium
Algorithms
Open
Approved
Problem
50% Success 490 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code

Nam draws a tree in a paper (Note: tree here is a sinple connected acyclic graph in Graph theory, not tree in reality). The tree consists of N nodes. Nam want to makes his tree more beautiful. Therefore, he decided to coloring all N nodes.

Nam numbering nodes from 1 to N for convinent. He firstly color node 1. Then he will color N - 1 remaining nodes, in any order which satisfied condition: node are chosen to color must be adjacent to one of nodes colored before. Two nodes are consider adjacent if there is a direct edge between them.

Nam wondering, how many ways of coloring the tree he possible make. Two ways are consider different if order of coloring nodes in 2 ways are different, because Nam uses only 1 color.

Input

The first line is T - the number of test cases. Then T test cases follow.

Each test consists of several lines.

  • The first line is N - the number of nodes in tree.
  • Then the next N - 1 lines, each line contains 2 integer u and v (1 ≤ u, vN), denoting there is a direct edge between node u and node v. It is guaranteed that these edges form a tree.

Output

For each test, print the number of ways coloring the tree in a single line. Since this number can very large, you must print it modulo 109 + 7.

Constraints

  • 1 ≤ T ≤ 10
  • 1 ≤ N ≤ 100000

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:10
330 votes
Tags:
ApprovedEasyImplementationReady
Points:20
290 votes
Tags:
MathematicsOpenApprovedEasyProbability and Statistics
Points:30
36 votes
Tags:
Digit DPDynamic ProgrammingAlgorithmsAdvancedMath