#P4591. [TJOI2018] 碱基序列

[TJOI2018] 碱基序列

Description

Xiaodou joined a biology laboratory. In the lab, he mainly studies proteins. The protein he is currently studying consists of kk amino acids in a fixed order. For each amino acid ii, there are aia_i possible base sequences si,js_{i,j}.

Now Xiaodou has a base string ss, and he wants to know how many different combinations could produce this protein on this base string. That is, consider the string s1s_1 obtained by concatenating kk string segments in order, where for the ii-th segment you choose one sequence from its aia_i options. Count how many different ways s1s_1 can match the string ss as a contiguous substring. Two ways are considered different if either the choices for the kk segments differ, or the matched positions in ss differ.

Input Format

  • The first line contains an integer indicating that the protein consists of kk amino acids.
  • The second line contains a string ss, which is the current base string.
  • The next kk lines describe the possible base sequences for the ii-th amino acid. For the ii-th amino acid, the line starts with aia_i, the number of possible base sequences, followed by aia_i strings representing these possible base sequences, separated by spaces.

Output Format

Output a single integer: the number of different ways (either different substrings of ss, or for the same substring, different choices of sequences for the amino acids). The answer is taken modulo 109+710^9+7.

2
ABC
2 A AB
2 C BC
2
2
AAA
2 A AA
2 A AA
4

Hint

Sample 1 Explanation

  • First choose A\tt A, second choose C\tt C, obtaining AC\tt AC, which matches ABC\tt ABC in 00 ways.
  • First choose A\tt A, second choose BC\tt BC, obtaining ABC\tt ABC, which matches ABC\tt ABC in 11 way.
  • First choose AB\tt AB, second choose C\tt C, obtaining ABC\tt ABC, which matches ABC\tt ABC in 11 way.
  • First choose AB\tt AB, second choose BC\tt BC, obtaining ABBC\tt ABBC, which matches ABC\tt ABC in 00 ways.

Therefore there are 22 ways in total.

Sample 2 Explanation

  • First choose A\tt A, second choose A\tt A, obtaining AA\tt AA, which matches AAA\tt AAA in 22 ways.
  • First choose A\tt A, second choose AA\tt AA, obtaining AAA\tt AAA, which matches AAA\tt AAA in 11 way.
  • First choose AA\tt AA, second choose A\tt A, obtaining AAA\tt AAA, which matches AAA\tt AAA in 11 way.
  • First choose AA\tt AA, second choose AA\tt AA, obtaining AAAA\tt AAAA, which matches AAA\tt AAA in 00 ways.

Therefore there are 44 ways in total.

Constraints

  • For 30%30\% of the testdata, 1k251 \le k \le 25, 1s100001 \le |s| \le 10000, 1ai31 \le a_i \le 3.
  • For 100%100\% of the testdata, 1k1001 \le k \le 100, 1s100001 \le |s| \le 10000, 1ai101 \le a_i \le 10. The length of each base sequence does not exceed 1515. The character set consists of uppercase letters.

Translated by ChatGPT 5