Codeforces Round 1056 (Div. 2)
C
The Ancient Wizards' Capes
Codeforces
1600
原题
← BD →
翻译中…

There are $n$ wizards in a row numbered $1$ to $n$ from left to right. Each wizard has an invisibility cape which can be worn either on his left side or on his right side. Harry walks from the position of wizard $1$ until the position of wizard $n$ ($1 \le n \le 10^5$), and registers how many wizards he sees from each wizard's position. A wizard in position $j$ is visible from position $i$ if:

翻译中…
  • Wizard $j$ wears his cape on his left side and $i \ge j$.
  • Wizard $j$ wears his cape on his right side and $i \le j$.
翻译中…

In particular, note that wizard $i$ is visible from position $i$.

翻译中…

Harry's list is very old but, after much work, you managed to decipher it. The list is an array $a$ of $n$ elements, where the $i$-th element $a_i$ ($1 \le a_i \le n$) is the number of wizards that Harry saw from the position of wizard $i$.

翻译中…

Your task is to determine how many of all the possible cape arrangements that Harry could have seen are consistent with the data recorded by the list, modulo $676\,767\,677$.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

翻译中…

The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) — the length of $a$.

翻译中…

The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$) — the elements of $a$.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.

翻译中…

Output

翻译中…

For each test case, print one integer — the number of arrangements for the wizards' capes that satisfy the condition, modulo $676\,767\,677$.

翻译中…

Note

翻译中…

The image below shows an arrangement of capes that matches Harry's list in the second test case.

翻译中…

image

翻译中…

Wizard 1 has the invisibility cape on his left side, while wizards $2$, $3$, and $4$ wear it on their right side.

翻译中…
  • From position $1$, we can see wizards $1$, $2$, $3$, and $4$.
  • From position $2$, we can see wizards $1$, $2$, $3$, and $4$.
  • From position $3$, we can see wizards $1$, $3$, and $4$.
  • From position $4$, we can see wizards $1$ and $4$.
翻译中…

Thus, Harry's list ends up being $[4, 4, 3, 2]$. It can be proved that this is the only possible arrangement.

翻译中…

In the third test case, it can be proved that Harry could not have obtained his list from any cape arrangement.

翻译中…

In the fifth case, note that there are two possible cape arrangements from which Harry could have gotten his list:

翻译中…
  • $1 \mid \quad \mid 2 \quad 3 \, \mid$
  • $\mid \, 1 \quad 2 \mid \quad \mid 3$
翻译中…

样例

Input #1
7
1
1
4
4 4 3 2
3
1 3 2
2
2 1
3
2 2 2
3
3 2 3
3
3 2 2
Output #1
2
1
0
1
2
0
0
InputCopy OutputCopy
题目信息
题号
2155C
时间限制
2 s
内存限制
256 MB
标签
暴力
贪心
模拟

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。