#nt702. Power Tower

Power Tower

Power Tower

Problem Source

Codeforces Round #454 (Div. 1) D

Problem Description

You are given nn numbers a1,a2,…,ana_1, a_2, \dots, a_n and qq queries. For each query, given an interval [l,r][l, r], you must compute alal+1…ar mod ma_l^{a_{l+1} ^{\dots^{a_r}}} \bmod m.

Input Format

The first line contains two integers n,mn, m (1≤n≤1051 \leq n \leq 10^5, 1≤m≤1091 \leq m \leq 10^9).

The next line contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤1091 \leq a_i \leq 10^9).

The next line contains an integer qq (1≤q≤1051 \leq q \leq 10^5). The following qq lines each contain two integers l,rl, r (1≤l≤r≤n1 \leq l \leq r \leq n).

Output Format

Output qq lines, each containing one number: the answer for that query.

Sample Input

6 1000000000
1 2 2 3 3 3
8
1 1
1 6
2 2
2 3
2 4
4 4
4 5
4 6

Sample Output

1
1
2
4
256
3
27
597484987