| <p> | |
| <strong>N</strong> pairs of friends are standing in two lines of <strong>N</strong> people each, waiting to go through an airport security checkpoint. | |
| The <em>i</em>th passenger in the first line (counting from the front) belongs to pair <strong>A<sub>i</sub></strong>, | |
| while the <em>i</em>th passenger in the second line belongs to pair <strong>B<sub>i</sub></strong>. | |
| In the 2<strong>N</strong> integers <strong>A<sub>i..N</sub></strong>, and <strong>B<sub>i..N</sub></strong>, | |
| each value from 1 and <strong>N</strong> appears exactly twice. A pair of friends might be standing in the same line or in different lines. | |
| </p> | |
| <p> | |
| Every minute, the first person in one of the two lines will be admitted through security. | |
| If one of the lines is already empty, then the person at the front of the other line will necessarily be admitted. | |
| This process will go on for 2<strong>N</strong> minutes, until both lines have been exhausted. | |
| Some people are much slower than others at the tedious process of removing their shoes and belts, placing their laptops in separate bins, and so on. | |
| As such, it's unclear which line will be chosen to advance in each minute. | |
| </p> | |
| <p> | |
| When a passenger passes through security, they will wait just past the checkpoint for their friend (the other person in their pair) to also make it through. | |
| If their friend has already made it through first, then the two of them will immediately proceed to their gate. | |
| As such, if a pair of friends pass through security after <strong>a</strong> and <strong>b</strong> minutes respectively, | |
| they will be able to head to their gate after max(<strong>a</strong>, <strong>b</strong>) minutes. | |
| However, everyone hates standing around after security for too long! | |
| If anyone is forced to wait for their friend for more than 2 minutes | |
| (that is, max(<strong>a</strong>, <strong>b</strong>) > min(<strong>a</strong>, <strong>b</strong>) + 2), | |
| then they'll throw a fit and the entire airport will be closed down. | |
| </p> | |
| <p> | |
| Assuming that everyone is satisfied and manages to get through security without closing down the airport, | |
| sorting the pairs of friends by the times at which they proceed to their gates yields a permutation of <strong>N</strong> pair numbers. | |
| How many such pair orders are possible (modulo 1,000,000,007)? | |
| </p> | |
| <p> | |
| In order to reduce the size of your input file, you're given <strong>A<sub>1</sub></strong>, and <strong>A<sub>2..N</sub></strong> can be calculated as follows: | |
| </p> | |
| <p> | |
| <strong>A<sub>i</sub></strong> = <strong>A<sub>i-1</sub></strong> + <strong>D<sub>A,i-1</sub></strong> | |
| </p> | |
| <p> | |
| In order to compute the sequence <strong>D<sub>A,1..(N-1)</sub></strong>, you're given <strong>K<sub>A</sub></strong> other sequences, the <em>i</em>th of which consists of <strong>L<sub>A,i</sub></strong> elements <strong>S<sub>A,i,1..L<sub>A,i</sub></sub></strong>, and has an associated repetition number <strong>R<sub>A,i</sub></strong>. The sequence <strong>D<sub>A,1..(N-1)</sub></strong> can then be constructed by concatenating together <strong>K<sub>A</sub></strong> sequences, the <em>i</em>th of which consists of the sequence <strong>S<sub>A,i,1..L<sub>A,i</sub></sub></strong> repeated <strong>R<sub>A,i</sub></strong> times. It's guaranteed that the concatenation of these repeated sequences has exactly <strong>N</strong> - 1 elements (in other words, the sum of the products <strong>L<sub>A,i</sub></strong> * <strong>R<sub>A,i</sub></strong> for <em>i</em> = 1..<strong>K<sub>A</sub></strong> is equal to <strong>N</strong> - 1). | |
| </p> | |
| <p> | |
| In the same way, you're given <strong>B<sub>1</sub></strong>, and <strong>B<sub>2..N</sub></strong> can be calculated using the sequence <strong>D<sub>B,1..(N-1)</sub></strong>, which in turn can be calculated by concatenating together <strong>K<sub>B</sub></strong> sequences, the <em>i</em>th of which consists of <strong>R<sub>B,i</sub></strong> copies of the sequence <strong>S<sub>B,i,1..L<sub>B,i</sub></sub></strong>. | |
| </p> | |
| <h3>Input</h3> | |
| <p> | |
| Input begins with an integer <strong>T</strong>, the number of different airports. | |
| For each airport, there is first a line containing the integer <strong>N</strong>. | |
| </p> | |
| <p> | |
| There is then a line with two space-separated integers <strong>A<sub>1</sub></strong> and <strong>K<sub>A</sub></strong>. | |
| Then there are <strong>K<sub>A</sub></strong> lines, the <em>i</em>th of which contains two space-separated integers <strong>R<sub>A,i</sub></strong> and <strong>L<sub>A,i</sub></strong>, followed by <strong>L<sub>A,i</sub></strong> more space-separated integers, the <em>j</em>th of which is <strong>S<sub>A,i,j</sub></strong>. | |
| </p> | |
| <p> | |
| Similarly, there is then a line with two space-separated integers <strong>B<sub>1</sub></strong> and <strong>K<sub>B</sub></strong>. | |
| Then there are <strong>K<sub>B</sub></strong> lines, the <em>i</em>th of which contains two space-separated integers <strong>R<sub>B,i</sub></strong> and <strong>L<sub>B,i</sub></strong>, followed by <strong>L<sub>B,i</sub></strong> more space-separated integers, the <em>j</em>th of which is <strong>S<sub>B,i,j</sub></strong>. | |
| </p> | |
| <h3>Output</h3> | |
| <p> | |
| For the <em>i</em>th airport, print a line containing "Case #<strong>i</strong>: " | |
| followed by the number of possible pair orders, modulo 1,000,000,007. If it's not possible for everybody to get through security without somebody throwing a tantrum, output 0. | |
| </p> | |
| <h3>Constraints</h3> | |
| <p> | |
| 1 ≤ <strong>T</strong> ≤ 400 <br /> | |
| 2 ≤ <strong>N</strong> ≤ 2,000,000 <br /> | |
| 1 ≤ <strong>A<sub>i</sub></strong>, <strong>B<sub>i</sub></strong>, <strong>K<sub>A</sub></strong>, <strong>K<sub>B</sub></strong>, <strong>R<sub>A,i</sub></strong>, <strong>R<sub>B,i</sub></strong>, <strong>L<sub>A,i</sub></strong>, <strong>L<sub>B,i</sub></strong> ≤ <strong>N</strong> <br /> | |
| -<strong>N</strong> ≤ <strong>D<sub>A,i</sub></strong>, <strong>D<sub>B,i</sub></strong>, <strong>S<sub>A,i,j</sub></strong>, <strong>S<sub>B,i,j</sub></strong> ≤ <strong>N</strong> <br /> | |
| </p> | |
| <h3>Explanation of Sample</h3> | |
| <p> | |
| In the first case, A = [1, 3, 2] and B = [3, 1, 2]. The two possible pair orders are [1, 3, 2] and [3, 1, 2]. One way to achieve the former is to admit passengers from lines 1, 2, 2, 1, 1, and 2 (who belong to pairs 1, 3, 1, 3, 2, and 2). | |
| </p> | |
| <p> | |
| In the second case, A = B = [1, 2, 3, 4, 5]. There is only one possible pair order: [1, 2, 3, 4, 5]. | |
| </p> | |
| <p> | |
| In the third case, A = [2, 12, 5, 12, 5, 7, 1, 7, 4, 4, 15, 10, 15, 10, 14] and B = [6, 6, 8, 8, 11, 2, 11, 13, 9, 13, 9, 3, 3, 1, 14]. | |
| </p> | |
| <p> | |
| In the fourth case, A = [1, 2, 3, 4, 5] and B = [5, 4, 3, 2, 1]. There's no way for all of these people to get through security without causing a ruckus. | |
| </p> | |
| <p> | |
| In the fifth case, A = [1, 1, 2, 2, ..., 4999, 4999, 5000, 5000] and B = [5001, 5001, 5002, 5002, ..., 9999, 9999, 10000, 10000]. | |
| </p> | |