시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 512 MB306725225.616%

문제

You are given a function $f \, : \, \mathbb{N}_0 \to \mathbb{N}_0$ defined as below. ($\mathbb{N}_0$ denotes the set of non-negative integers.)

  • $f(0) = a_0$
  • $f(i) = a_i^{f(i - 1)}$ ($i \ge 1$)

Find the value of $f(n)$ modulo $m$.

입력

The first line contains a single integer $n$.

The second line contains $n + 1$ integers $a_0, \, a_1, \, \cdots, \, a_n$.

The third line contains a single integer $m$.

출력

Print the value of $f(n)$ modulo $m$.

제한

  • $0 \le n \le 2$
  • $1 \le a_i \le 2 \times 10^9$ ($0 \le i \le n$)
  • $2 \le m < 2 \times 10^9$; $m$ is a prime number.
  • All values in input are integers.

예제 입력 1

2
2 3 2
11

예제 출력 1

6

$f(0) = 2$, $f(1) = 3^2 = 9$, $f(2) = 2^9 = 512$.

The value of $512$ modulo $11$ is $6$.

노트

As a note, the exact value of $9^{9^9}$ has a whopping $369 \, 693 \, 700$ digits.

출처