| At last, your power knows no bounds! Within the world of the ever-popular | |
| role-playing game _Dungeons & Dragons_, or _D&D_, you've managed to become one | |
| of the most powerful wizards to have ever wizarded. | |
| And yet, you're still not content — you feel compelled to continue grinding | |
| away, repeatedly destroying monsters to ever so slowly increase your power | |
| further. There's a particular cave which contains **N** zombies, and these | |
| zombies respawn every time you reenter the cave. As such, you can efficiently | |
| gain experience by entering, destroying all of them, and leaving, repeating | |
| this process infinitely (or until your supply of Hot Pockets runs out). The | |
| _n_th zombie has a power level of _n_. | |
| Conveniently, you're also carrying **N** wands with you, the _n_th of which | |
| has a power level of _n_. Each time you enter the cave, you'll use each of | |
| your wands exactly once to cast a spell and destroy a single zombie, such that | |
| all **N** zombies are defeated. Initially, you know **N** different spells, | |
| the _n_th of which allows your wand with power level _n_ to target the zombie | |
| with power level _n_. | |
| You're going to visit the cave **M** times today. Before each visit, you're | |
| going to learn some new spells (which you'll continue to be able to use for | |
| all of your remaining visits). In particular, before your _i_th visit, you'll | |
| acquire **Si** different new spells, each of which allows your wand with power | |
| level **Wi** to target the zombie with power level of **Zi**. Wands are unable | |
| to affect zombies which are much more powerful than them, and it would be a | |
| waste to use a wand on a zombie much weaker than it. As such, |**Wi** \- | |
| **Zi**| ≤ 1 holds for all spells. Note that all spells are considered | |
| distinct, even if several of them allow a particular wand to target a | |
| particular zombie. | |
| On each of your **M** visits to the cave, you're interested in the number of | |
| different ways in which you can defeat all **N** zombies, using only the | |
| spells that you've learned up to that point. Two ways are considered different | |
| if at least one spell is used in one of them but not the other. These | |
| quantities can get rather large, and you don't want to deal with **M** big | |
| numbers, so you only care about computing their sum modulo 1,000,000,007. | |
| You're given **W1**, and **W2..M** may then be calculated as follows, using | |
| given constants **Aw** and **Bw**: | |
| **Wi** = ((**Aw** * **Wi-1** \+ **Bw**) % **N**) + 1 | |
| A temporary sequence of values **D1..M** will be used to help calculate | |
| **Z1..M**. You're given **D1**, and **D2..M** may then be calculated as | |
| follows, using given constants **Ad** and **Bd**: | |
| **Di** = (**Ad** * **Di-1** \+ **Bd**) % 3 | |
| From these values, **Z1..M** may then be calculated as follows: | |
| **Zi** = max{ 1, min { **N**, **Wi** \+ **Di** \- 1} } | |
| Finally, you're given **S1**, and **S2..M** may then be calculated as follows, | |
| using given constants **As** and **Bs**: | |
| **Si** = ((**As** * **Si-1** \+ **Bs**) % 1,000,000,000) + 1 | |
| ### Input | |
| Input begins with an integer **T**, the number of different caves you'll | |
| visit. For each cave, there are four lines. The first line contains the two | |
| space-separated integers **N** and **M**. The second line contains the three | |
| space-separated integers **W1**, **Aw**, and **Bw**. The third line contains | |
| the three space-separated integers **D1**, **Ad**, and **Bd**. The fourth line | |
| contains the three space-separated integers **S1**, **As**, and **Bs**. | |
| ### Output | |
| For the _i_th cave, print a line containing "Case #**i**: " followed by one | |
| integer. This number is the sum (taken modulo 1,000,000,007) of **M** values, | |
| the _j_th of which is the number of different ways in which you can defeat the | |
| zombies on your _j_th visit to the cave. | |
| ### Constraints | |
| 1 ≤ **T** ≤ 20 | |
| 1 ≤ **N**, **M** ≤ 800,000 | |
| 1 ≤ **W1** ≤ **N** | |
| 0 ≤ **Aw**, **Bw** ≤ **N** \- 1 | |
| 0 ≤ **D1**, **Ad**, **Bd** ≤ 2 | |
| 1 ≤ **S1**, **As**, **Bs** ≤ 1,000,000,000 | |
| ### Explanation of Sample | |
| In the first case, your first learn a new spell that can defeat the first | |
| zombie with your first wand. This gives you 2 ways to defeat the first zombie | |
| with your first wand, and 1 way to defeat the second zombie with your second | |
| wand, for a total of 2 ways to defeat both. | |
| You then learn three new ways to defeat the second zombie with your second | |
| wand. 2 ways to defeat the first zombie and 4 ways to defeat the second zombie | |
| is 8 ways to defeat both. | |
| You then learn five new ways to defeat the second zombie with your first wand. | |
| You can't actually use your first wand to defeat the second zombie though, as | |
| that will leave you with no ways to defeat the first zombie. So you still have | |
| 8 ways to defeat both. | |
| Finally, you learn seven new ways to defeat the first zombie with the second | |
| wand. If you use the first wand on the first zombie, and the second wand on | |
| the second zombie, you still have 8 combinations of possible spells. If you | |
| use the first wand on the second zombie and second wand on the first zombie, | |
| you have 7 * 5 = 35 combinations of possible spells. In total, that's 43 ways | |
| to defeat the zombies. | |
| Therefore the total answer is 2 + 8 + 8 + 43 = 61. | |