#P5270. 无论怎样神树大人都会删库跑路

无论怎样神树大人都会删库跑路

Background

As everyone knows, Divine J (Joker) goes to Chengdu FZ every few days pretending to attend classes, but is actually playing with pointers. Divine J can, when nobody is paying attention, pull out a pointer and point himself to any position (a living person suddenly appearing in a specimen cabinet?), or swap the pointers of two people (the Chengdu FZ version of Your Name?), or chant system commands at the OJ to make it randomly slow down (mcfx: why did the CPU get slower after boosting?).

Lord Divine Tree is very unhappy, because a tree must stay in place, and Lord Divine Tree also does not know pointers. But Lord Divine Tree is a god, so he plans to delete the database of this universe and run away, so that the idle Divine J can only play cards with Lord Divine Tree.

Problem Description

There is a string SS of length TT and nn small strings aia_i.

You are given an array RR of length mm, indexed starting from 1. Initially, there is an empty string XX. Lord Divine Tree will perform QQ operations. In the ii-th operation, he appends the small string aR(i1)modm+1a_{R_{(i-1)\bmod m+1}} to the end of XX.

After each operation, Lord Divine Tree checks whether the string XX has a suffix such that, after an arbitrary permutation, it can become SS.

Ask how many times the string XX has a suffix that can become SS after an arbitrary permutation (that is, the counts of all characters are the same).

Unfortunately, the character set size of this string is as large as 10510^5, so you must read the strings as integer arrays.

Input Format

Input n,T,Qn, T, Q.

Then input TT numbers representing the string SS.

Then input nn lines. In each line, the first number lenlen is the length, followed by lenlen numbers representing this small string. Each input number is in the range [0,105][0,10^5].

Then input mm.

Input one line with mm numbers representing RR.

Output Format

Output the answer.

5 5 20
2 2 0 2 0
2 2 0
2 0 2
3 0 2 0
3 0 2 0
2 2 2
10
2 1 5 5 2 2 4 2 5 3
6
10 10 10000
0 1 1 1 0 1 1 0 0 0 
6 0 0 1 1 1 0 
6 0 0 0 0 0 0 
5 0 0 0 0 0 
4 1 0 0 0 
5 1 1 1 0 1 
2 1 1 
6 0 0 0 0 0 1 
1 0 
4 0 0 1 1 
1 1 
30
10 4 3 9 10 9 4 8 5 10 9 8 6 10 10 4 9 2 2 9 6 4 1 10 10 1 9 10 3 5 
3001

Hint

Explanation for Sample 1

Constraints

For all testdata, n,T,m105n, T, m \leq 10^5, 1Rin1 \leq R_i \leq n, Q109Q \leq 10^9. The total length of all small strings does not exceed 10510^5. All characters are in [0,105][0,10^5].

Subtask nn TT QQ mm Special Properties
1 (20 points) n10n \le 10 T10T \le 10 Q100Q \le 100 m10m \le 10 The alphabet is [0,5][0,5], and the total length of all small strings does not exceed 100100.
2 (30 points) n103n \le 10^3 T100T \le 100 Q109Q \le 10^9 m103m \le 10^3 The alphabet is [0,5][0,5], Ri=iR_i = i, n=mn = m, and the total length of all small strings does not exceed 100000100000.
3 (10 points) n105n \le 10^5 T105T \le 10^5 m105m \le 10^5 The alphabet is [0,5][0,5], Ri=iR_i = i, n=mn = m.
4 (40 points)

Translated by ChatGPT 5