Yeongwoo and God4

Given a character and a sequence of monsters generated by recurrence, decide whether the character survives all N fights in order and print the final stats or -1.

Medium6SimulationMathImplementationNumber theoryNo attempts yetTime limit1.5sMemory limit512 MB

Problem

Yeongwoo is playing a game called God4 again. In God4 every character and every monster has an attack AA, a defence DD, and a maximum health HH. Combat is always one on one and runs by these rules.

  1. On your turn you attack the other side. The defender loses the larger of (attacker's attack - defender's defence) and 11 health.
  2. When the attack ends, the turn passes to the other side.
  3. The two sides keep taking turns until one side's health drops to 00 or below. The side whose health drops first loses.
  4. Yeongwoo's character always attacks first.

When the character wins, it absorbs the monster's stats exactly as they are. A character with stats (5,5,5)(5, 5, 5) that beats a monster with stats (1,1,1)(1, 1, 1) ends up with (6,6,6)(6, 6, 6). Every win also restores the character's health to its maximum, so every fight starts at full health.

Yeongwoo wants to beat NN monsters in a row without a single loss, which means he has to win every fight. Listing the data of all NN monsters would make the input far too large, so the stats of the kk-th monster for k>1k > 1 come from these formulas.

Ak=((Ak1×Ap+Aa)mod100)+1A_k = ((A_{k-1} \times A_p + A_a) \bmod 100) + 1

Dk=((Dk1×Dp+Da)mod3)+1D_k = ((D_{k-1} \times D_p + D_a) \bmod 3) + 1

Hk=((Hk1×Hp+Ha)mod1000)+1H_k = ((H_{k-1} \times H_p + H_a) \bmod 1000) + 1

Input

The program reads from standard input.

The first line contains the number of monsters NN. (1N5×1071 \le N \le 5 \times 10^7)

The second line contains the attack AA, the defence DD, and the maximum health HH of Yeongwoo's character, in that order. (1A1001 \le A \le 100, 1D31 \le D \le 3, 1H10001 \le H \le 1000)

The third line contains the attack A1A_1, the defence D1D_1, and the maximum health H1H_1 of the first monster, in that order. (1A11001 \le A_1 \le 100, 1D131 \le D_1 \le 3, 1H110001 \le H_1 \le 1000)

The fourth line contains the values ApA_p, AaA_a, DpD_p, DaD_a, HpH_p, HaH_a used to generate the monster stats, in that order. (1Ap,Dp,Hp1091 \le A_p, D_p, H_p \le 10^9, 0Aa990 \le A_a \le 99, 0Da20 \le D_a \le 2, 0Ha9990 \le H_a \le 999)

Output

The program writes to standard output.

If Yeongwoo beats all NN monsters, print the character's final stats on one line in the order attack, defence, maximum health, separated by single spaces. Print each of the three values modulo 109+710^9 + 7.

If he loses even once, print -1 on the first line and nothing else.