Decompose the Tree

All submissions for this problem are available.
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
You are given an unrooted tree. There is an integer written on each node of this tree.
Your task is to count the number of ways to decompose this tree to a set of paths in such a way that each node belongs to exactly one path and the sum of numbers associated with all the nodes on any of these path is always nonnegative.
Input
The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains a single integer N denoting the number of nodes.
The second line contains N spaceseparated integers A_{1}, A_{2}, ... , A_{N} denoting the numbers written on the corresponding nodes.
Each of the following (N1) lines contain a pair of spaceseparated integers X_{j} Y_{j} denoting that there is an edge between the nodes numbered X_{j} and Y_{j}.
Output
For each test case, output a single line containing the number of ways to decompose the tree to a set of paths, under the given condition, modulo 10^{9}+7.
Constraints
 1 ≤ T ≤ 10^{5}
 1 ≤ X_{j}, Y_{j} ≤ N
Subtask 1 (15 points):
 1 ≤ sum of N over test case ≤ 10^{4}
 1 ≤ N ≤ 100
 10 ≤ A_{i} ≤ 10
Subtask 2 (16 points):
 1 ≤ sum of N over test case ≤ 10^{5}
 1 ≤ N ≤ 10^{3}
 10^{4} ≤ A_{i} ≤ 10^{4}
Subtask 3 (69 points):
 1 ≤ sum of N over test case ≤ 10^{5}
 1 ≤ N ≤ 10^{5}
 10^{4} ≤ A_{i} ≤ 10^{4}
Example
Input: 1 4 1 10 5 1 1 2 1 3 2 4 Output: 4
Explanation
Let's list all the four possible decompositions:
 The whole tree is one path. Then, the sum of nodes' associated numbers is 1 + 10 + 5 + (1) = 15.
 One path consists of the nodes with the indices 2 and 4, another one consists of the nodes 1 and 3. Then, the sum of nodes' associated numbers in the first path is 10 + (1) = 9 and the sum of nodes' numbers in the second path is 1 + 5 = 6.
 One path consists of the nodes with the indices 1, 2 and 4, another one consists of the node 3. Then, the sum of nodes' associated numbers in the first path is 1 + 10 + (1) = 10 and the sum of nodes' numbers in the second path is 5.
 The first path consists of the nodes with the indices 2 and 4, the second one consists of the node 1, and the third consists of the node 3. Then, the sum of nodes' associated numbers in the first path is 10 + (1) = 9, the sum of nodes' numbers in the second path is 1, the sum of nodes' numbers in the third path is 5.
Author:  xcwgf666 
Tester:  iscsi 
Editorial  http://discuss.codechef.com/problems/TREEPATH 
Tags  bit, fenwick, fenwicktree, hard, nov15, xcwgf666 
Date Added:  27092015 
Time Limit:  2 sec 
Source Limit:  50000 Bytes 
Languages:  C, CPP14, JAVA, PYTH, PYTH 3.6, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, SCALA, 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, PERL6, TEXT, SCM chicken, CLOJ, FS 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions