Crystal

No attempts yetTime limit1sMemory limit128 MB

Problem

Bajtazar works at a company that manufactures crystals. Each atom of a crystal is one of three kinds, written A0A_0, A1A_1, A2A_2. A crystal of order nn consists of 3n(n1)+13n(n-1)+1 atoms arranged into a regular hexagon with side n1n-1, which is divided into 6(n1)26(n-1)^2 equilateral triangles of side 11. The picture below shows an example crystal of order 22.

Order-2 crystal example

Crystals have an electrical property. Any three atoms that form a triangle of side 11 and whose kinds are pairwise different produce a field of unit charge: positive if the atoms appear in the order A0A_0, A1A_1, A2A_2 when the triangle is traversed counterclockwise, or negative if that same order appears when the triangle is traversed clockwise. The charge of the whole crystal is the sum of the charges of all such triples.

Bajtazar's machine is programmed with six numbers nn, mm, ss, aa, bb, kk. It builds the crystal of order nn, placing atoms row by row starting from the topmost row, and left to right within each row. The kind ArA_r of each successive atom is chosen by running the following steps in order:

s := ((a * s + b) div k) mod (3 * m)
r := s div m

Here div is integer division (the quotient) and mod is the remainder. The two statements run one after another, once for each atom.

Compute the charge of the crystal produced for the given input parameters.

Input

The first line contains six integers nn, mm, ss, aa, bb, kk describing the machine's input parameters (2n1092 \le n \le 10^9, 1m1061 \le m \le 10^6, 0s,a,b<3m0 \le s, a, b < 3m, 1k<3m1 \le k < 3m).

Output

Print one integer: the charge of the crystal the machine produces.