Adjacent leaves

All submissions for this problem are available.
Read problems statements in Russian and Vietnamese as well.
beauty of a rooted tree is defined as the number of good subsets of leaves.
a leaf in rooted tree is a node that doesn't have children.
suppose you do dfs from the root, whenever you visit a leaf you write down the label of the leaf in a sequence, leaves are written in order of visiting. consider all possible sequences that can created using dfs traversal (different sequences can come from different order of visiting children of some node), we call subset of leaves good iff it's not empty and there's at least one sequence such that leaves of that subset appear consecutively (next to each other, any order is fine) in that sequence.
Given a tree of N nodes, nodes are numbered from 1 to N, for every node i find the beauty of the tree if node i was the root of the tree. since the answer is large output it modulo 10^{9}+7
Input
First line of input contains one integer T denoting the number of testcases
First line of each testcase contains N denoting number of nodes
Each of the following N  1 lines contains two integers u v denoting that there's an edge between nodes u and v
Output
For each testcase output N lines, in ith line output a single integer denoting the beauty of the tree if node i was the root modulo 10^{9}+7
Constraints
 1 ≤ T ≤ 1,000
 2 ≤ N ≤ 300,000
 2 ≤ sum of N in all testcases ≤ 300,000
Example
Input: 2 5 1 2 2 3 2 4 1 5 10 1 2 1 3 1 4 2 5 2 6 3 7 3 8 4 9 4 10 Output: 7 7 3 3 3 55 51 51 51 27 27 27 27 27 27
Explanation
Example case 1. all subsets of leaves are good whatever the root is, so the answer is 7 when there are 3 leaves and 3 when there are 2 leaves
Example case 2. when the root is node 1 all subsets of leaves are good except:
 5, 7, 9
 5, 7, 10
 5, 8, 9
 5, 8, 10
 6, 7, 9
 6, 7, 10
 6, 8, 9
 6, 8, 10
so the answer is 63  8 = 55
Author:  kingofnumbers 
Tester:  amrmahmoud 
Tags  cook88, dynamicprogramming, hard, kingofnumbers 
Date Added:  17112017 
Time Limit:  2 sec 
Source Limit:  50000 Bytes 
Languages:  C, CPP14, JAVA, PYTH, PYTH 3.5, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, rust, SCALA, swift, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, kotlin, PERL6, TEXT, SCM chicken, CLOJ, COB, FS 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions
HELP
If you are still having problems, see a sample solution here. 