This page is still under construction.

Parts of this page are still being built. What you see may change.

Sequence Artisan

Interview

Time limit1sMemory limit256 MB

Summary
Find the largest product of any contiguous block in an array with values from -2 to 2, then report it modulo 1000000007.
Level

Medium5 of 10

Topics
Greedy, Dynamic programming, Math
Solved
No attempts yet

Problem

Seunghyun builds sequences a1,a2,…,aNa_1, a_2, \dots, a_N of length NN for customers who need one. He is early in his career, so he can only build sequences whose elements are integers between −2-2 and 22, and few customers come to him.

Jihak ordered a sequence from him. From the finished sequence Jihak picks one contiguous block ai,ai+1,…,aj−1,aja_i, a_{i+1}, \dots, a_{j-1}, a_j (1≤i≤j≤N1 \le i \le j \le N) and wants the product of its elements, ai×ai+1×⋯×aj−1×aja_i \times a_{i+1} \times \dots \times a_{j-1} \times a_j, to be as large as possible. If i=ji = j, the product is aia_i.

Given the sequence Seunghyun built, find the largest product Jihak can obtain.

Input

The first line contains the number of test cases TT (1≤T≤1000001 \le T \le 100000). The TT test cases follow.

The first line of each test case contains the length of the sequence NN (2≤N≤1000002 \le N \le 100000). The second line contains the elements a1,a2,…,aNa_1, a_2, \dots, a_N (−2≤ai≤2-2 \le a_i \le 2), separated by spaces.

The sum of NN over all test cases does not exceed 300000300000.

Output

For each test case, print the largest product modulo 1,000,000,0071{,}000{,}000{,}007 (=109+7= 10^9 + 7) on its own line.

Compare the true products when choosing the largest one, then reduce only that chosen value modulo 109+710^9 + 7.

Examples4

  1. Example 1

    Input
    3
    5
    1 2 -1 2 1
    5
    1 2 -1 2 -1
    2
    2 -1
    
    Expected output
    2
    4
    2
    
  2. Example 2

    Input
    4
    5
    0 0 0 0 0
    3
    -1 0 -2
    2
    0 2
    6
    2 0 -2 -2 0 1
    
    Expected output
    0
    0
    2
    4
    
  3. Example 3

    Input
    5
    2
    2 -2
    2
    -1 -1
    2
    1 1
    2
    -2 1
    2
    0 0
    
    Expected output
    2
    1
    1
    1
    0
    
  4. Example 4

    Input
    3
    5
    -1 -1 -1 -1 -1
    4
    -2 -2 -2 -2
    7
    -2 1 -2 0 -2 -2 2
    
    Expected output
    1
    16
    8