Shades of the town

Given several integer patterns and one shade array, count every occurrence where a contiguous block equals a pattern scaled by a positive real factor.

Hard8String matchingMathNo attempts yetTime limit1sMemory limit1024 MB

Problem

A long time ago, in a galaxy far away, there was a town. The town is gone and only its shade is left.

Assume that every building in the town stood on a single line at equal spacing. All buildings had the same width and differed only in height. The buildings are gone and only their shadows remain. A shadow length does not have to equal the original building height. All shadows may have been scaled by the same positive constant.

We want to know the architecture of the civilization that lived on this planet. You are given several sequences of building heights, called patterns. For each pattern, find every place where it appears in the original row of buildings.

You are given a sequence of positive integers, the lengths of the preserved shadows. You are also given several queries, and one query gives one pattern. A pattern is a sequence of positive integers, the heights of some buildings. A pattern appears in the shade if some contiguous block of the shadow sequence is exactly the pattern multiplied by a positive real factor.

Compute the total number of appearances over all patterns. Appearances are allowed to overlap. A pattern of length 11 can be scaled to any value, so it appears at every position of the shade.

Input

The first line contains the number of patterns nn.

Each of the next nn lines describes one pattern. The line starts with the pattern length lil_i, followed by lil_i positive integers separated by spaces.

The last line describes the shade. It starts with the shade length mm, followed by mm positive integers separated by spaces.

Constraints:

  • 1n1 \le n
  • 1m3×1051 \le m \le 3 \times 10^5
  • 1li1 \le l_i
  • li3×105\sum l_i \le 3 \times 10^5
  • Every height and every shadow length in the input is between 11 and 1000010000, inclusive.

Output

Print the total number of appearances of all patterns in the shade as a single integer on one line.