#P1097H. Mateusz and an Infinite Sequence

    ID: 4831 Type: RemoteJudge 7000ms 512MiB Tried: 0 Accepted: 0 Difficulty: 10 Uploaded By: Tags>bitmasksbrute forcedpstrings*3400

Mateusz and an Infinite Sequence

Description

A Thue-Morse-Radecki-Mateusz sequence (Thorse-Radewoosh sequence in short) is an infinite sequence constructed from a finite sequence gen\mathrm{gen} of length dd and an integer mm, obtained in the following sequence of steps:

  • In the beginning, we define the one-element sequence M0=(0)M_0=(0).
  • In the kk-th step, k1k \geq 1, we define the sequence MkM_k to be the concatenation of the dd copies of Mk1M_{k-1}. However, each of them is altered slightly — in the ii-th of them (1id1 \leq i \leq d), each element xx is changed to (x+geni)(modm)(x+\mathrm{gen}_i) \pmod{m}.

For instance, if we pick gen=(0,1,2)\mathrm{gen} = (0, \color{blue}{1}, \color{green}{2}) and m=4m = 4:

  • M0=(0)M_0 = (0),
  • M1=(0,1,2)M_1 = (0, \color{blue}{1}, \color{green}{2}),
  • M2=(0,1,2,1,2,3,2,3,0)M_2 = (0, 1, 2, \color{blue}{1, 2, 3}, \color{green}{2, 3, 0}),
  • M3=(0,1,2,1,2,3,2,3,0,1,2,3,2,3,0,3,0,1,2,3,0,3,0,1,0,1,2)M_3 = (0, 1, 2, 1, 2, 3, 2, 3, 0, \color{blue}{1, 2, 3, 2, 3, 0, 3, 0, 1}, \color{green}{2, 3, 0, 3, 0, 1, 0, 1, 2}), and so on.

As you can see, as long as the first element of gen\mathrm{gen} is 00, each consecutive step produces a sequence whose prefix is the sequence generated in the previous step. Therefore, we can define the infinite Thorse-Radewoosh sequence MM_\infty as the sequence obtained by applying the step above indefinitely. For the parameters above, M=(0,1,2,1,2,3,2,3,0,1,2,3,2,3,0,3,0,1,)M_\infty = (0, 1, 2, 1, 2, 3, 2, 3, 0, 1, 2, 3, 2, 3, 0, 3, 0, 1, \dots).

Mateusz picked a sequence gen\mathrm{gen} and an integer mm, and used them to obtain a Thorse-Radewoosh sequence MM_\infty. He then picked two integers ll, rr, and wrote down a subsequence of this sequence A:=((M)l,(M)l+1,,(M)r)A := ((M_\infty)_l, (M_\infty)_{l+1}, \dots, (M_\infty)_r).

Note that we use the 11-based indexing both for MM_\infty and AA.

Mateusz has his favorite sequence BB with length nn, and would like to see how large it is compared to AA. Let's say that BB majorizes sequence XX of length nn (let's denote it as BXB \geq X) if and only if for all i{1,2,,n}i \in \{1, 2, \dots, n\}, we have BiXiB_i \geq X_i.

He now asks himself how many integers xx in the range [1,An+1][1, |A| - n + 1] there are such that B(Ax,Ax+1,Ax+2,,Ax+n1)B \geq (A_x, A_{x+1}, A_{x+2}, \dots, A_{x+n-1}). As both sequences were huge, answering the question using only his pen and paper turned out to be too time-consuming. Can you help him automate his research?

The first line contains two integers dd and mm (2d202 \leq d \leq 20, 2m602 \leq m \leq 60) — the length of the sequence gen\mathrm{gen} and an integer used to perform the modular operations. The second line contains dd integers geni\mathrm{gen}_i (0geni<m0 \leq \mathrm{gen}_i < m). It's guaranteed that the first element of the sequence gen\mathrm{gen} is equal to zero.

The third line contains one integer nn (1n300001 \leq n \leq 30000) — the length of the sequence BB. The fourth line contains nn integers BiB_i (0Bi<m0 \leq B_i < m). The fifth line contains two integers ll and rr (1lr10181 \leq l \leq r \leq 10^{18}, rl+1nr-l+1 \geq n).

Print a single integer — the answer to the problem.

Input

The first line contains two integers dd and mm (2d202 \leq d \leq 20, 2m602 \leq m \leq 60) — the length of the sequence gen\mathrm{gen} and an integer used to perform the modular operations. The second line contains dd integers geni\mathrm{gen}_i (0geni<m0 \leq \mathrm{gen}_i < m). It's guaranteed that the first element of the sequence gen\mathrm{gen} is equal to zero.

The third line contains one integer nn (1n300001 \leq n \leq 30000) — the length of the sequence BB. The fourth line contains nn integers BiB_i (0Bi<m0 \leq B_i < m). The fifth line contains two integers ll and rr (1lr10181 \leq l \leq r \leq 10^{18}, rl+1nr-l+1 \geq n).

Output

Print a single integer — the answer to the problem.

Sample Input 1

2 2
0 1
4
0 1 1 0
2 21

Sample Output 1

6

Sample Input 2

3 4
0 1 2
2
0 2
6 11

Sample Output 2

1

Note

Thorse-Radewoosh sequence in the first example is the standard Thue-Morse sequence, so the sequence AA is as follows: 1101001100101101001011010011001011010010. Here are the places where the sequence BB majorizes AA: