Particle Experiment
InterviewTime limit1.5sMemory limit512 MB
Count the ways to place 1x2 switches on an R by C grid so every particle reaches the sensor positive, modulo 1e9+7.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Bit manipulation
- Solved
- No attempts yet
Problem
Albert studies small particles. Each particle has either a positive (+) or a negative (-) charge, and a lab apparatus can flip these charges. The apparatus is a grid of size that stands upright. Albert fires one particle from the top of each column, at row 1, and a sensor below row detects it.
For example, in the figure below, , , and the string = +-++ means that particles are fired from columns 1 to 4 in the order positive, negative, positive, positive. Each particle travels in a straight line through all cells, from row 1 to row , and reaches the sensor at the bottom.

Some cells of the apparatus have preinstalled switches. A particle that passes through such a cell changes from positive to negative or from negative to positive. Let be the number of preinstalled switches, and let and be the row and column index of the -th switch.
In the figure below, , , and . Every detected particle is positive. The particle in column 2 changes from negative to positive. The particle in column 4 changes to negative and then back to positive.

Albert also has many switches, which cover two horizontally adjacent cells. He wants every particle to be positive when it reaches the sensor. Each switch must always cover two cells side by side, and no cell may be covered by more than one switch. For the example above, there are three ways to reach the goal:
- Place switches at row 2, columns 1-2, and at row 3, columns 1-2.
- Place switches at row 2, columns 2-3, and at row 3, columns 2-3.
- Add no switches.

In this example, the two switches are labeled AA and BB for convenience. All switches look identical, so they cannot be told apart.
Another example is , , . Albert has five different ways to reach the goal.

Given , , , , and , count the ways to add switches to the apparatus so that every particle is positive when it reaches the sensor. The answer can be very large, so print it modulo .
Input
The first line contains the number of test cases .
The first line of each test case contains , , and , separated by spaces. The second line contains a string of length , which gives the charge of the particle fired from each column at the start. The next lines each contain and , separated by a space.
Output
Print the answer for each test case on its own line.
Constraints
- For every with , and .
- Each pair is unique within a test case, so no cell is given more than once.
- Assume Albert has enough switches to use as many as he needs.