It is well known that Wesley is bad at dynamic programming. Six months after the contest, Wesley learned that you can solve the original problem with a significantly better time complexity. He decided to create a new problem with tighter constraints. The only differences between this problem and the original are the bounds on
You are given a tree with
Wesley originally wanted you to output the full answer, but he decided to be nice and only ask you to output it modulo
For this question, a connected subgraph is a subset of the original vertices and edges that form a tree.
Constraints
The graph is a tree.
Input Specification
The first line contains 2 integers,
The next line contains a string of R
or B
. If the R
, then vertex
The next
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
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
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
Comments