Under construction

Still being built. Things may change

Particle Experiment

Interview

Time limit1.5sMemory limit512 MB

Summary
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 R×CR \times C that stands upright. Albert fires one particle from the top of each column, at row 1, and a sensor below row RR detects it.

For example, in the figure below, R=3R = 3, C=4C = 4, and the string SS = +-++ 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 RR cells, from row 1 to row RR, and reaches the sensor at the bottom.

Some 1×11 \times 1 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 KK be the number of preinstalled switches, and let XiX_i and YiY_i be the row and column index of the ii-th switch.

In the figure below, K=3K = 3, X=[1,1,2]X = [1, 1, 2], and Y=[2,4,4]Y = [2, 4, 4]. 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 1×21 \times 2 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 R=2R = 2, C=4C = 4, K=0K = 0. Albert has five different ways to reach the goal.

Given RR, CC, KK, XX, and YY, count the ways to add 1×21 \times 2 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 109+710^9 + 7.

Input

The first line contains the number of test cases TT.

The first line of each test case contains RR, CC, and KK, separated by spaces. The second line contains a string SS of length CC, which gives the charge of the particle fired from each column at the start. The next KK lines each contain XiX_i and YiY_i, separated by a space.

Output

Print the answer for each test case on its own line.

Constraints

  • 1≤T≤51 \le T \le 5
  • 1≤R≤5001 \le R \le 500
  • 1≤C≤101 \le C \le 10
  • 0≤K≤min⁡(R×C,1000)0 \le K \le \min(R \times C, 1000)
  • For every ii with 1≤i≤K1 \le i \le K, 1≤Xi≤R1 \le X_i \le R and 1≤Yi≤C1 \le Y_i \le C.
  • Each pair (Xi,Yi)(X_i, Y_i) 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.

Examples2

  1. Example 1

    Input
    5
    3 4 3
    +-++
    1 2
    1 4
    2 4
    2 4 0
    ++++
    1 6 0
    ++--++
    1 6 0
    +++++-
    3 6 3
    +-+--+
    1 1
    2 3
    2 5
    
    Expected output
    3
    5
    1
    0
    8
    
  2. Example 2

    Input
    1
    16 4 0
    ++++
    
    Expected output
    78867036