Periodic points#1820

Periodic points

20 seconds
256 megabytes
Very easy

Computing the number of fixed points and, more generally, the number of periodic orbits within a dynamical system is a question of interest in various fields of research. However, the dynamics may be very complicated even in seemingly simple models. In this task, you are to compute the number of periodic points of period for a piecewise linear map mapping the real interval into itself. That is, given a map f : [0, m] \rightarrow [0, m], you must calculate the number of solutions to the equation f^n(x) = x for x \in [0, m], where f^n denotes the -fold composition of , i.e.

where denotes composition of maps, (gh)(x) = g(h(x)).

Fortunately, the maps in this problem have the following properties:

Figure 1: Graphs of the third sample map, f_3 (left), and its square, f_3^2 (right).

Since there may be many periodic points, output the result modulo an integer.

Input

The input consists of several test cases, separated by single blank lines. Each test case begins with a line containing the integer (1 \leq m \leq 80). The next line describes the map ; it contains m+1 integers f(0), f(1), \ldots, f(m), each between and inclusive. The test case ends with a line containing two integers separated by a space: (1 \leq n \leq 5\ 000) and the modulus for the result, (2 \leq \text{mod} \leq 10\ 000).

The input ends with a line containing '0'.

Output

For each test case, output the number of solutions to f^n(x) = x in the interval modulo . If there are infinitely many solutions, print 'Infinity'.

Examples

Example 1

Loading
Loading