For this question, Python users are recommended to use PYPY over CPython.
You are given a tree with vertices. Recall that a tree is a connected graph where there is exactly one path between any two vertices. Each vertex in this tree is assigned a colour, red or black. You are asked to determine the number of balanced subgraphs with exactly vertices. A subgraph is considered to be balanced if all vertices are connected and there are at least red vertices and black vertices.
. It may be helpful to know that is prime and .
originally wanted you to output the full answer, but since his birthday is coming up, he decided to be nice and only ask you to output it moduloFor this question, a connected subgraph is a subset of the original vertices and edges that form a tree.
Constraints
For this problem, you will NOT be required to pass all the samples in order to receive points. In addition, all subtasks are disjoint, and you are NOT required to pass previous subtasks to earn points for a specific subtask.
Subtask  Points  

 
 

For all subtasks:
The graph is a tree.
Input Specification
The first line contains 2 integers, and .
The next line contains a string of characters, describing the colouring of the tree. Each character is either R
or B
. If the character is R
, then vertex is red. Otherwise, it is black.
The next lines describe the edges of the tree. Each line contains 2 integers, , , indicating an edge between and .
Output Specification
This problem is graded with an identical
checker. This includes whitespace characters. Ensure that every line of output is terminated with a \n
character and that there are no trailing spaces.
Output a single integer, the number of balanced subgraphs with exactly vertices, modulo .
Sample Input 1
6 5
BBBRBR
1 2
2 3
2 4
3 5
2 6
Sample Output 1
2
Sample Explanation 1
The two balanced subgraphs of size are are and .
Sample Input 2
5 4
RBRBR
1 2
2 3
2 4
2 5
Sample Output 2
3
Sample Explanation 2
The three balanced subgraphs of size are , , and .
Comments
this question made me angry