#P11573. Light Bulb Shutdown Sequences

    ID: 13665 Type: Default 1000ms 256MiB

Light Bulb Shutdown Sequences

Light Bulb Shutdown Sequences

You are given n light bulbs arranged in a row numbered from 1 to n. The i-th light bulb produces a power of \(a_i\) watts when lit.

You want to turn off all the bulbs using the following greedy algorithm:

  1. Select a starting position \(p\) and turn off bulb \(p\).
  2. Let the current position be \(x\). While there is at least one bulb still on, do:
    1. If there is a remaining bulb only to the left of \(x\), let \(l\) be the closest bulb on the left of \(x\). Turn off bulb \(l\) and update \(x \gets l\).
    2. If there is a remaining bulb only to the right of \(x\), let \(r\) be the closest bulb on the right of \(x\). Turn off bulb \(r\) and update \(x \gets r\).
    3. If there are remaining bulbs both to the left and right of \(x\), let \(l\) be the closest bulb on the left and \(r\) be the closest bulb on the right.
      • If \(a_l = a_r\), then you may choose either bulb (this introduces a randomness in the process).
      • If \(a_l \neq a_r\), then turn off the bulb with the larger power.
      After turning off the chosen bulb \(y\) (either \(l\) or \(r\)), update \(x \gets y\).

Define \(f(p)\) as the number of different shutdown sequences produced by the above greedy algorithm when the starting position is \(p\). You are given \(m\) queries with positions \(x_1, x_2, \dots, x_m\). For each query, output \(f(x_i) \bmod (10^9+7)\).

Note: Two sequences are considered different if the order of bulbs being turned off is different.

inputFormat

The first line contains two integers \(n\) and \(m\) — the number of light bulbs and the number of queries.

The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) where \(a_i\) is the power of the \(i\)-th bulb.

The third line contains \(m\) integers \(x_1, x_2, \dots, x_m\), representing the starting positions for the queries.

outputFormat

Output \(m\) integers. The \(i\)-th integer should be \(f(x_i) \bmod (10^9+7)\).

sample

3 3
3 3 3
1 2 3
1 2 1