-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCookingGame.java
More file actions
83 lines (66 loc) · 1.96 KB
/
CookingGame.java
File metadata and controls
83 lines (66 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package problems.codechef;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Created by hardCode on 2/20/2017.
*/
public class CookingGame {
final static int MAX=100000,M=1000000007;
static int[]a=new int[MAX];
static BufferedReader br;
public static void main(String[] args) throws IOException {
br=new BufferedReader(new InputStreamReader(System.in));
int t,n;
String[]s;
t=Integer.parseInt(br.readLine());
while (t-->0){
n=Integer.parseInt(br.readLine());
readArray(a,n);
System.out.println(solve(a,n));
}
}
private static long solve(int[] a, int n) {
if(a[0]==-1)a[0]=1;
if (a[0]!=1)return 0;
//This functions fills the position which can be determined, if(xenny[i+1]>1) then xenny[i] has to be xenny[i+1]-1
if (fillDeterminedPositions(a,n)==0)return 0;
int k=0;
for (int i = 0; i < n; i++) {
if (a[i]==-1)k++;
}
/*
for (int i = 0; i < n; i++) {
System.out.print(xenny[i] + " ");
}
System.out.println();
*/
return binaryExponentiation(2,k);
}
public static long binaryExponentiation(long base,long exp){
long ans=1;
while (exp>0){
if (exp%2==1)
ans=(ans*base)%M;
base=(base*base)%M;
exp>>=1;
}
return ans;
}
private static int fillDeterminedPositions(int[] a, int n) {
for (int i =n-2 ; i>=0 ; i-- ) {
if (a[i+1]>1){
if (a[i]!=a[i+1]-1 && a[i]>-1)return 0;
if (a[i]==-1)a[i]=a[i+1]-1;
}
}
return 1;
}
private static void readArray(int[] a, int n) throws IOException {
String[]s;
s=br.readLine().split("\\s");
for (int i = 0; i < n; i++) {
a[i]=Integer.parseInt(s[i]);
}
}
}