Learn DSADSAContests
coding75 logo
LeetCode POTDPOTDSheets
coding75 Pro
coding75 logo
Dashboard
Learn DSA
Course RoadmapFlow
DSA Topic TreeNew πŸš€
Contest SolutionsPractice
Practice Sheets
MasterclassesLive πŸ‘¨πŸ»β€πŸ’»

coding75 ProPRO

Live Classes & Placement Guidance

coding75 logo

The premier developer platform for mastering Data Structures & Algorithms, exploring contest editorials, building ATS-ready resumes, and accelerating your tech career.

Connect & Community

DSA & Contests

  • Learn DSA
  • Contest Solutions
  • LeetCode POTDDaily
  • Practice Sheets
  • MasterclassesSoon

Interview Prep

  • Portfolio Projects
  • CS Fundamentals
  • System DesignSoon
  • Interview ExperiencesSoon
  • Mock InterviewsSoon

Career & Pro

  • Jobs & Internships
  • Resume BuilderATS
  • coding75 Pro
  • Submit Feedback
Β© 2026coding75β€’crackDSAβ„’β€’Maa Lalita Edtech Private Limited. All Rights Reserved.
Privacy PolicyTerms & ConditionsContact Support
Registered Office: Kanpur 208021β€’Regional Office: Indiranagar, Bangalore, 560008
Maa Lalita Edtech Private Limited
Contests/Codeforces/Codeforces Round 1056/C. The Ancient Wizards' Capes
Prev
CodeforcesCodeforces
Expert
Codeforces Round 1056

C. The Ancient Wizards' Capes

Prefix SumArraySuffix ArrayProbability and StatisticsMath
Loading...
Solve on Codeforces

Problem Statement

Official algorithmic problem description and constraints.

Open on Codeforces

There are π‘›

 wizards in a row numbered 1

 to π‘›

 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 π‘›

 (1≀𝑛≀10

5

), and registers how many wizards he sees from each wizard's position. A wizard in position π‘—

 is visible from position π‘–

 if:

  • Wizard π‘—
  •  wears his cape on his left side and π‘–β‰₯𝑗
  • .
  • Wizard π‘—
  •  wears his cape on his right side and π‘–≀𝑗
  • .

In particular, note that wizard π‘–

 is visible from position π‘–

.

Harry's list is very old but, after much work, you managed to decipher it. The list is an array π‘Ž

 of π‘›

 elements, where the π‘–

-th element π‘Ž

𝑖

 (1β‰€π‘Ž

𝑖

≀𝑛

) is the number of wizards that Harry saw from the position of wizard π‘–

.

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 676767677

.

Input


Each test contains multiple test cases. The first line contains the number of test cases π‘‘

 (1≀𝑑≀10

4

). The description of the test cases follows.

The first line of each test case contains a single integer π‘›

 (1≀𝑛≀10

5

) β€” the length of π‘Ž

.

The second line contains π‘›

 integers π‘Ž

1

,π‘Ž

2

,…,π‘Ž

𝑛

 (1β‰€π‘Ž

𝑖

≀𝑛

) β€” the elements of π‘Ž

.

It is guaranteed that the sum of π‘›

 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 676767677

.

Example

Input

Copy

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

Copy

2
1
0
1
2
0
0

Note


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

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∣∣23∣
  • ∣12∣∣3


Solutions & Walkthrough

Detailed video explanations, mathematical intuition, and clean C++ implementation code.

Connecting secure classroom stream...
Back to Codeforces Round 1056
Previous Problem