Turning off the bulbs
Time limit1sMemory limit512 MB
Count the possible turn-off orders when an interval expands from a start bulb by always taking the stronger of the two frontier bulbs, branching on ties.
- Level
Hard8 of 10
- Topics
- Combinatorics, Two pointers, Math
- Solved
- No attempts yet
Problem
Every morning, when the first sunlight appears, Ivan has to turn off all the light bulbs in the street lights of his village. The village has a single straight road, and the street lights stand on one side of it, numbered 1 to from left to right.
Each day Ivan picks a starting street light and turns off its bulb first. After that he saves electricity by following one rule. At every step he looks at the nearest bulb that is still on to the left and the nearest bulb that is still on to the right, and turns off the one with the bigger power. If no bulb is on to the left, he turns off the nearest bulb on the right, and if no bulb is on to the right, he turns off the nearest bulb on the left. If the two bulbs have equal power, Ivan can turn off either one of them. That is why the same starting position can produce several different orders of turning the bulbs off.
Let be the number of different orders when Ivan starts at . For the given starting positions, compute modulo .
Input
The first line contains the number of street lights and the number of starting positions , separated by a space.
The second line contains the powers of the bulbs , separated by spaces.
The third line contains the starting positions , separated by spaces. The same position can appear more than once.
Output
Print lines. The -th line contains modulo .
Constraints
Note
Consider five street lights with powers . If Ivan starts at street light 3, he turns off bulb 3 and reaches . The left bulb has the bigger power, so he turns off bulb 2 and reaches . Now the right bulb has the bigger power, so he turns off bulb 4 and reaches . Bulbs 1 and 5 have equal power, so either one can go first and the order splits into two.
Starting at street light 5 in the same village, no bulb is ever on to the right, so the only order turns the bulbs off from right to left.