406067: GYM102253 L Limited Permutation

Memory Limit:128 MB Time Limit:3 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

L. Limited Permutationtime limit per test3 secondsmemory limit per test128 megabytesinputstandard inputoutputstandard output

For a permutation $$$p_1$$$, $$$p_2$$$, $$$\ldots$$$, $$$p_n$$$ obtained from $$$1$$$ to $$$n$$$, it is uncomplicated for each $$$i$$$ to calculate $$$(l_i, r_i)$$$ meeting the condition that $$$\min(p_L, p_{L + 1}, \ldots, p_R) = p_i$$$ if and only if $$$l_i \leq L \leq i \leq R \leq r_i$$$.

Given the positive integers $$$n$$$, $$$(l_i, r_i)$$$, you are asked to calculate the number of possible permutations $$$p_1$$$, $$$p_2$$$, $$$\ldots$$$, $$$p_n$$$ obtained from $$$1$$$ to $$$n$$$, meeting the above condition.

The answer may be exceedingly large, so you should output the answer modulo $$$(10^9 + 7)$$$ instead.

Input

The input contains multiple test cases.

For each test case, the first line contains an integer $$$n$$$ ($$$1 \leq n \leq 10^6$$$).

The second line contains $$$n$$$ integers $$$l_1$$$, $$$l_2$$$, $$$\ldots$$$, $$$l_n$$$ ($$$1 \leq l_i \leq i$$$ for each $$$i$$$).

The third line contains $$$n$$$ integers $$$r_1$$$, $$$r_2$$$, $$$\ldots$$$, $$$r_n$$$ ($$$i \leq r_i \leq n$$$ for each $$$i$$$).

It is guaranteed that the sum of $$$n$$$ in all test cases is no more than $$$3 \times 10^6$$$.

Output

For each test case, output "Case #x: y" in one line (without quotes), where $$$x$$$ indicates the case number starting from $$$1$$$, and $$$y$$$ denotes the answer to the corresponding case.

ExampleInput
3
1 1 3
1 3 3
5
1 2 2 4 5
5 2 5 5 5
Output
Case #1: 2
Case #2: 3

加入题单

算法标签: