#P8273. [USACO22OPEN] Pair Programming G

[USACO22OPEN] Pair Programming G

Background

Due to issues with the testdata, in this problem you do not need to consider non-trivial cases where two different groups of numbers have the same product (cases are considered trivial if they both contain 00, or differ only by some number of 11's, or only differ in order), for example t×2×3=t×6t\times2\times3=t\times6. Alternatively, you should treat ×2,3,4,5,6,7,8,9\times 2,3,4,5,6,7,8,9 in the statement as ×2,3,5,7,11,13,17,19\times 2,3,5,7,11,13,17,19, respectively.

Problem Description

A program consists of a sequence of instructions, and each instruction has one of the following forms:

  • ×d\times d, where dd is a single digit in the range [0,9][0,9].
  • +s+s, where ss is a string representing a variable name. All variable names appearing in a program are distinct.

The result of executing a program is defined as the expression obtained by applying each instruction in order to the expression 00. For example, executing the program [×3,+x,+y,×2,+z][\times 3,+x,+y,\times 2,+z] produces 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 after execution; for example, executing [+w,×0,+y,+x,×2,+z,×1][+w,\times 0,+y,+x,\times 2,+z,\times 1] also produces the expression 2×x+2×y+z2\times x+2\times y+z.

Bessie and Elsie each have a program with NN instructions (1N20001\le N\le 2000). They will interleave the instructions of these programs to create a new program with 2N2N instructions. Note that there are (2N)!N!×N!\frac{(2N)!}{N!\times N!} ways to do this, but not all such programs produce different expressions after execution.

Compute the number of distinct expressions that can result from executing an interleaving of Bessie's and Elsie's programs, modulo 109+710^9+7.

Each test file contains TT subtestcases (1T101\le T\le 10) that should be solved independently. The input guarantees that the sum of NN over all subtestcases does not exceed 20002000.

Input Format

The first line of input contains TT, the number of subtestcases.

For each subtestcase, the first line contains NN.

The second line of each subtestcase contains Bessie's program, represented as a string of length NN. Each character is either a single 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 subtestcase contains Elsie's program, in the same format as Bessie's program.

Within a subtestcase, all variable names in all instructions are distinct. Note that their actual names are not given here, because they do not affect the answer.

Output Format

Output the number of distinct expressions that can result from executing an interleaving of Bessie's and Elsie's programs, modulo 109+710^9+7.

4
1
0
1
3
12+
+02
3
0++
++9
4
5+++
+6+1
1
3
9
9

Hint

【Sample Explanation】

For the first subtestcase, the two interleavings that can be created are [×1,×0][\times 1, \times 0] and [×0,×1][\times 0,\times 1]. After execution, both produce the expression 00.

For the second subtestcase, executing the interleavings of [×1,×2,+x][\times 1,\times 2, +x] and [+y,×0,×2][+y, \times 0,\times 2] can produce one of the expressions 00, xx, and 2×x2\times x.

【Test Point Properties】

  • Test point 2 satisfies N6N\le 6.
  • In test points 3-5, the sum of all NN does not exceed 100100.
  • In test points 6-8, the sum of all NN does not exceed 500500.
  • Test points 9-16 have no additional constraints.

Translated by ChatGPT 5