Pair Programming
Time limit2sMemory limit1024 MB
Count the distinct expressions formed by interleaving two programs of N instructions each, modulo 10^9+7.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Combinatorics, String
- Solved
- No attempts yet
Problem
A program consists of a sequence of instructions. Each instruction is one of the following forms:
- , where is a digit in the range
- , where is a string denoting the name of a variable. Within a program, all variable names must be distinct.
The result of executing a program is the expression that results after applying each instruction in order, starting with . For example, the result of executing the program is the expression . Different programs may produce the same expression when executed. For example, executing also results in the expression .
Bessie and Elsie each have a program of () instructions. They interleave these programs to produce a new program of length . There are ways to do this, but not all of them produce distinct expressions when executed.
Count the number of distinct expressions that may be produced by executing Bessie and Elsie's interleaved program, modulo .
Input
The first line of the input contains , the number of test cases. Each test case is solved independently, with , and the sum of over all test cases does not exceed .
The first line of each test case contains .
The second line of each test case contains Bessie's program, a string of length . Each character is either a digit , representing an instruction of type 1, or the character , representing an instruction of type 2.
The third line of each test case contains Elsie's program in the same format as Bessie's.
Within a test case, the variable names among all instructions are distinct. Their actual names are not provided, since they do not affect the answer.
Output
Print the number of distinct expressions that may be produced by executing Bessie and Elsie's interleaved programs, modulo .
Hint
For the first test case, the two possible interleaved programs are and . Both produce the expression when executed.
For the second test case, executing an interleaving of and could produce one of the expressions , , or .