This page is still under construction.

Parts of this page are still being built. What you see may change.

Pair Programming

Time limit2sMemory limit1024 MB

Summary
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:

  1. ×d\times d, where dd is a digit in the range [0,9][0,9]
  2. +s+s, where ss 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 00. For example, the result of executing the program [×3,+x,+y,×2,+z][\times 3,+x,+y,\times 2,+z] is the expression (0×3+x+y)×2+z=2×x+2×y+z(0\times 3+x+y)\times 2+z=2\times x+2\times y+z. Different programs may produce the same expression when executed. For example, executing [+w,×0,+y,+x,×2,+z,×1][+w,\times 0,+y,+x,\times 2,+z,\times 1] also results in the expression 2×x+2×y+z2\times x+2\times y+z.

Bessie and Elsie each have a program of NN (1≤N≤20001\le N\le 2000) instructions. They interleave these programs to produce a new program of length 2N2N. There are (2N)!N!×N!\frac{(2N)!}{N!\times N!} 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 109+710^9+7.

Input

The first line of the input contains TT, the number of test cases. Each test case is solved independently, with 1≤T≤101\le T\le 10, and the sum of NN over all test cases does not exceed 20002000.

The first line of each test case contains NN.

The second line of each test case contains Bessie's program, a string of length NN. Each character is either a digit d∈[0,9]d\in [0,9], 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 109+710^9+7.

Hint

For the first test case, the two possible interleaved programs are [×1,×0][\times 1, \times 0] and [×0,×1][\times 0,\times 1]. Both produce the expression 00 when executed.

For the second test case, executing an interleaving of [×1,×2,+x][\times 1,\times 2, +x] and [+y,×0,×2][+y, \times 0,\times 2] could produce one of the expressions 00, xx, or 2×x2\times x.

Examples1

  1. Example 1

    Input
    4
    1
    0
    1
    3
    12+
    +02
    3
    0++
    ++9
    4
    5+++
    +6+1
    
    Expected output
    1
    3
    9
    9