#P9768. [ROIR 2021] A+B (Day 2)

[ROIR 2021] A+B (Day 2)

Background

Translated from ROIR 2021 Day 2 T4 A+B

Problem Description

There are three integers a,b,ca, b, c of length nn, which may contain leading zeros. They are arranged into three rows and nn columns as follows:

a
b
c

How many different ways are there to permute the columns so that the three integers read horizontally, x,y,zx, y, z, satisfy x+y=zx + y = z, and none of the three integers has leading zeros.

The number of permutations may be large; output it mod 109+7\bmod \ 10^9+7.

Input Format

The first line is an integer aa of length nn.

The second line is an integer bb of length nn.

The third line is an integer cc of length nn.

Output Format

Output one integer: the number of different permutations modulo 109+710^9+7.

123
123
246
6
01
02
03
1
01211
12099
23300
4
121
214
999
0

Hint

[Sample Explanation 1]: All permutations are valid.

[Sample Explanation 2]: We only count 10+20=3010 + 20 = 30, and we do not count 01+02=0301 + 02 = 03, because 0303 has a leading zero.

[Sample Explanation 3]: Clearly, there are two valid equations: 10121+21909=3203010121 + 21909 = 32030 and 12101+20919=3302012101 + 20919 = 33020. However, since there are two identical columns, each of them has two ways to obtain the answer. The total number of valid permutations is 2×2=42 \times 2 = 4.

[Constraints]:

For all subtasks, 2n2×1052 \le n \le 2 \times 10^5.

Subtask ID Special Constraints Score
11 n6n \le 6 77
22 n18n \le 18 1414
33 n200n \le 200, the input numbers do not contain 00 1515
44 n200n \le 200 55
55 n750n \le 750, the input numbers do not contain 00 1717
66 n750n \le 750 55
77 the input numbers do not contain 00 2020
88 no special constraints 1717

Translated by ChatGPT 5