We need to count number of binary matrices (h x w) with given row prefixes r_i (the number of consecutive full cells from the left) and column prefixes c_j (the number of consecutive full cells from the top). For each row i, the first r_i cells must be full, and the (r_i+1)-th cell (if exists) must be empty. Similarly for each column j, the first c_j cells must be full, and the (c_j+1)-th cell must be empty. For cells beyond these prefixes, they can be either full or empty subject to consistency.
But careful: The definitions: "r_i is the number of consecutive full cells connected to the left side in the i-th row. In particular, r_i = 0 if the leftmost cell is empty." So it means that the row starts with a block of full cells from column 1 to r_i inclusive (if r_i>0), and then after that if r_i < w, the cell at column r_i+1 must be empty (to break the consecutive block). However, it's ambiguous: could there be more full cells later? The problem says: "r_i is the number of consecutive full cells connected to the left side in the i-th row." Usually that means the maximal prefix of full cells? Actually reading: "the i-th row starts exactly with r_i full cells." So yes, exactly r_i full cells at the beginning, and then the next cell (if exists) must be empty. So the pattern in row i: positions 1..r_i are forced full; position r_i+1 (if <= w) forced empty; positions > r_i+1 are free? But careful: If r_i = w, then row is all full. Then no forced empty after.
Similarly, column j: positions 1..c_j forced full; position c_j+1 (if <= h) forced empty; positions > c_j+1 are free.
Thus we have a set of forced cells (full or empty). But there may be conflicts: if a cell is forced full by row condition but forced empty by column condition (or vice versa), then no solution (0). Otherwise, we need to count the number of assignments to the remaining cells (that are not forced by any condition) consistent with both? Wait, do the conditions impose any further restrictions? The definitions only specify the prefix conditions. So after the forced prefix empty cells, the rest of the row/column are unrestricted, except they must not affect the prefix counts. But is there any hidden condition? For example, if row i has r_i = 2, that means the first two cells are full, third is empty, but could the fourth be full? Yes, because the consecutive block from left is only the first two. So after the forced empty at column 3, we can have anything. Similarly for column j, after its forced empty, anything goes. So the only constraints are the forced assignments.
Therefore, the problem reduces to: Given h rows, each row forces first r_i cells to be 1, and if r_i < w, then cell (i, r_i+1) forced 0. Given w columns, each column forces first c_j cells to be 1, and if c_j < h, then cell (c_j+1, j) forced 0. All these forced assignments must be consistent. Then the cells that are not forced can be arbitrarily 0 or 1. So number of solutions = 2^(number of free cells) modulo MOD, if no conflict; else 0.
But is that always correct? Let's check with examples.
Example 1:
h=3, w=4
r = [0,3,1]
c = [0,2,3,0]
Let's compute forced cells.
Row1: r1=0 => leftmost empty. So (1,1) forced 0. Since r1=0 <4, then (1,1) is forced 0 (that matches leftmost empty). Also the definition: If r_i=0, then the leftmost cell is empty, but does it force anything else? The "number of consecutive full cells" is 0, so there is no full cell at the start. It doesn't say anything about cell at column 2. So only forced: position 1 forced empty? Actually check: For row i, if r_i = 0, then there are 0 full cells at start, meaning the first cell is empty. So (i,1) must be empty. But does it force anything beyond? The condition that "the i-th row starts exactly with r_i full cells" implies that the first r_i cells are full, and the next cell (if exists) is empty. So for r_i=0, the first cell must be empty, because we have 0 full cells, and then the next cell (if any) would be? Actually the rule "first r_i cells are full" when r_i=0 is vacuously true; then "the (r_i+1)-th cell is empty" => the first cell is empty. So yes, for r_i=0, (i,1) is forced empty. So row1: (1,1)=0 forced; other cells free? But wait: The rule "starts exactly with r_i full cells" might also imply that there cannot be a full cell before an empty? Actually it's just that the first block of consecutive full cells from the left is exactly r_i. For r_i=0, it means there is no full cell at the beginning; so the first cell is empty. However, could there be full cells later? Yes, because the row could have full cells after an empty. So that's correct.
Row2: r2=3 => (2,1),(2,2),(2,3) full; and since 3<4, (2,4) forced empty. Row2 columns: 1,2,3 =1; 4=0.
Row3: r3=1 => (3,1)=1; since 1<4, (3,2)=0 forced; columns >2 free.
Now columns:
c = [0,2,3,0]
Column1: c1=0 => (1,1) forced empty; (since 0<h=3, then (1,1) forced empty? Actually careful: For column j, first c_j cells full; then if c_j < h, cell (c_j+1, j) empty. So for c1=0: (1,1) forced empty (since c1+1=1); other cells free? So (1,1)=0 forced; consistent with row1's forced 0. Good.
Column2: c2=2 => (1,2),(2,2) full; since 2<3, (3,2) forced empty. So (1,2)=1, (2,2)=1, (3,2)=0.
But we already have from row3: (3,2) forced empty from row condition (since r3=1 => (3,2) forced 0). That's consistent. Also row2 (2,2) forced 1 matches. Row1 (1,2) wasn't forced from rows (since row1 only forced (1,1) empty), so we now have forced (1,2)=1 from column. OK.
Column3: c3=3 => (1,3),(2,3),(3,3) full; since 3= h? Actually h=3, c3=3 so it's full all the way; no forced empty because c3 = h, so no extra empty. So (1,3)=1, (2,3)=1, (3,3)=1 forced. Row2: (2,3) forced 1 matches; row3: (3,3) was free? But row3 forced (3,1)=1 and (3,2)=0; (3,3) is free originally, now forced 1 from column, ok.
Column4: c4=0 => (1,4) forced empty; since 0<3, (1,4) forced empty? Wait: c4=0 => first 0 cells full, then (c4+1)=1 => (1,4) empty. So (1,4)=0 forced. Row2: (2,4) forced 0 from row condition, consistent? Row2 forced (2,4)=0. Good. Row3: (3,4) free.
Now let's list forced assignments:
Row1: (1,1)=0; (1,2)=1; (1,3)=1; (1,4)=0 -> Actually (1,4)=0 from column. So row1 is fully determined? (1,1) forced 0, (1,2)=1, (1,3)=1, (1,4)=0. So row1 is completely forced; no free cells.
Row2: (2,1)=1 (row), (2,2)=1 (col), (2,3)=1 (row & col), (2,4)=0 (row). So fully forced.
Row3: (3,1)=1 (row), (3,2)=0 (row & col), (3,3)=1 (col), (3,4) free? Check column4: only forced (1,4)=0, (2,4)=0, but column4 c4=0 only forces (1,4)=0; after that, (2,4) and (3,4) are free (since c4+1=1, only that cell forced empty; cells below are free because column only forces prefix full and then one empty at c4+1; beyond that? Actually column j: first c_j cells are full; cell (c_j+1) is empty if c_j < h; cells > c_j+1 are unrestricted. So for column4, c4=0 => cell (1,4) empty; cells (2,4) and (3,4) are unrestricted. But row2 forced (2,4)=0, so that's a forced empty from row2, which is consistent (since it's unrestricted by column). Row3 column4 is unrestricted. However, we must also ensure that column4's condition is not violated by (2,4) or (3,4). Since column4 only requires that the first 0 cells are full (none) and that cell 1 is empty. It doesn't say anything about cell 2 or 3. So they can be anything. So (3,4) is free. Also check row3: after r3=1, forced (3,2)=0, but (3,3) and (3,4) are free. (3,3) got forced 1 by column3, so only (3,4) is free. Also (3,1) forced 1, (3,2) forced 0, (3,3) forced 1. So total free cells = 1. So number of solutions = 2^1 = 2. Output 2 matches.
So the simple approach works for example1.
Example2:
h=1, w=1
r=[0]
c=[1]
Row1: r1=0 => (1,1) forced empty.
Column1: c1=1 => (1,1) forced full.
Conflict -> 0. Output matches.
Example3 is large, but the approach yields some number modulo.
So it seems straightforward: compute forced values and check consistency, then count free cells.
But is it always true that the only constraints are these forced cells? Let's double-check for possible additional constraints: The definitions say "r_i is the number of consecutive full cells connected to the left side in the i-th row." Could it be that r_i is the maximum length of consecutive full cells starting from the left? The phrasing "the number of consecutive full cells connected to the left side" may be interpreted as the length of the contiguous block of full cells that touches the left border. That is essentially the same: the row begins with a block of full cells; that block's length is r_i. It could be that after an empty, there are more full cells, but those are not connected to the left side. So the condition only restricts the first r_i cells to be full and the (r_i+1)th to be empty (if exists). So it's exactly as we assumed.
Similarly for columns.
Thus solution: iterate over cells, determine if they are forced by row or column, check for conflicts, and count cells with no forced assignment.
But we must be careful: A cell might be forced by both row and column, but if they agree, it's fine.
But there's a nuance: The forced assignments from row conditions: For row i, positions 1..r_i must be full. For positions > r_i+1, no forced assignment. However, there is also the forced empty at position r_i+1 if r_i < w. That is a forced empty.
Similarly for column j.
Now, we must also ensure that the forced assignments from rows and columns do not violate each other's "unforced but implied" constraints? For instance, consider a cell that is not directly forced by row condition (i.e., beyond r_i+1), but might be forced by column condition. That's fine; it's a forced assignment from column. However, could there be a situation where the combination of forced assignments leads to an implicit constraint on another cell? For example, row i says first r_i cells are full, but suppose column j says that the first c_j cells are full, and cell (c_j+1, j) must be empty. That is just per column. But consider a cell (i,j) that is not directly forced by either, but its value could affect the row's "consecutive full cells connected to left" interpretation? Actually no, because row i's r_i is defined as the number of consecutive full cells from the left. That number is determined by the pattern of cells: it's the count of leading 1's until first 0 or end. So if we set cells arbitrarily beyond the forced positions, we must ensure that the actual leading 1 count matches the given r_i. But we already enforced that the first r_i cells are 1, and cell r_i+1 is 0 (if exists). This guarantees that the leading count is exactly r_i, regardless of later cells. Because the first r_i are 1, and if r_i < w, the (r_i+1)th is 0, so the block of consecutive 1's from left is exactly r_i (since it's broken by a 0). If r_i = w, all are 1, then the count is w. So the forced assignments are necessary and sufficient to guarantee the row condition. No other constraints from that row.
Similarly for columns.
Therefore, any assignment of the remaining cells, combined with forced ones, yields a grid satisfying both row and column conditions. So counting free cells is correct.
But careful: The forced assignments from row and column might overlap, but we need to ensure that if a cell is forced by both, they match. Also, there could be cells that are not forced by either directly, but could be indirectly forced because if a cell is not forced, but maybe there is some interaction: For example, suppose row i has r_i=2, then (i,3) is forced empty. That's fine. Column j might have c_j=2, then (3,j) forced empty. That's independent. But what about a cell (i,j) that is not forced by either, but if we set it to some value, could it affect the leading count of its row? No, because the leading count is already fixed by the forced cells at the beginning. For row i, the first three positions are forced: 1,1,0. So the leading 1's count is exactly 2 regardless of what happens in column 4,5,... So it's fine.
Thus the problem seems simple: count free cells = total cells (h*w) minus number of cells forced by row or column (or equivalently cells that have at least one forced condition). But careful: Some cells may be forced by both, but we count them once. So the number of free cells = total cells - |F| where F is set of cells that are forced either by row or column.
But we have to be careful: Are there cells that are forced by row condition but not in a simple sense? For row i, forced cells are columns 1..r_i (full), and if r_i < w, column r_i+1 (empty). So that's min(r_i, w?) Actually if r_i = w, then columns 1..w are forced full; there is no empty because r_i+1 > w. So forced cells per row: all columns 1..r_i (full), and if r_i < w, also column r_i+1 (empty). So number forced per row = r_i + (r_i < w ? 1 : 0). But careful: if r_i=0, forced cells: column 1 empty (since r_i < w usually, but maybe w=0? w>=1). So per row forced count = r_i + (r_i < w ? 1 : 0). But if r_i = w, then forced count = w (all full). Similarly per column j: forced rows = c_j + (c_j < h ? 1 : 0).
But the union of forced cells may be less than sum of these because of overlaps. However, we can compute forced set by marking cells. Since h,w ≤ 1000, we can create a grid of size up to 1e6, which is okay. But we can also compute the count analytically? Possibly, but marking is fine.
However, the challenge: The third example has h=19, w=16, which is tiny. So we could just simulate. But constraints say h,w up to 10^3, so grid up to 10^6, which is manageable. But we must also check for conflicts. So we can create a 2D array of chars or ints with initial -1 meaning unforced. Then apply row constraints: for each row i, for col=1..r_i, set cell(i,col)=1; if conflict (if already set to 0) -> conflict. Then for col = r_i+1 if ≤ w, set cell=0; check conflict. Then apply column constraints: for each column j, for row=1..c_j, set cell(row,j)=1; if conflict -> conflict; then for row = c_j+1 if ≤ h, set cell=0; conflict. Finally count cells that are still -1 (unforced). Answer = 2^(free) mod MOD.
But we need to be careful about the order: if we apply row constraints first and then column, we may detect conflicts when setting column constraints. So we can do both and check conflict.
This yields O(h*w) time and memory, which is fine for 1e6.
But is there any subtlety? Let's test with some corner cases.
Case: r_i = w, then row i forced full: columns 1..w = 1. No forced empty. Column constraints might set some of these to 0 if they conflict? For example, suppose h=2,w=2, r=[2,0], c=[0,2]. Row1: all full. Column1: c1=0 => (1,1) empty -> conflict with row1 full. So 0. Good.
Another case: r_i = w, but column c_j = h for some j, then all cells in that column forced full, consistent.
Now consider a case where a cell is forced empty by row and also forced empty by column, consistent. That's fine.
Now consider a cell that is not forced by either, but if we assign it a value, could it affect the column's leading count? For column j, the leading count is determined by the first cells from top: rows 1..c_j are forced full, and row c_j+1 (if exists) forced empty. So regardless of what we put in rows > c_j+1, the leading block length is c_j. So it's safe.
Thus solution is correct.
But wait, there's a nuance: The row condition says "r_i is the number of consecutive full cells connected to the left side in the i-th row." If there is a full cell after an empty, does that affect the "consecutive" condition? No, because the phrase "connected to the left side" likely means the contiguous block that touches the left border. So indeed the count is the length of that block. So forced as we did.
One more nuance: Could there be a scenario where the row forced cells and column forced cells are consistent, but the actual leading count of a row becomes something else because of some cell that is forced by column but affects the row's leading block? For row i, the leading block is determined by the first cell that is empty (or end). We set the first r_i cells to full and (if r_i<w) the (r_i+1)-th to empty. That guarantees the leading block is exactly r_i. But what if one of the first r_i cells is forced empty by column? That would cause conflict because row requires it full. So we detect conflict. Similarly, if (r_i+1)-th cell is forced full by column, conflict. So fine.
Thus solution is trivial.
But wait, the problem seems too easy for a typical competitive programming problem (maybe it's from Codeforces? I recall a problem like "r and c" but it had more complexity. Actually I think there is a known problem: "Masha and two friends"? No. There is a Codeforces problem "r and c" or something like that? I recall a problem: Given r_i and c_j, count number of grids where each row starts with r_i full cells and each column starts with c_j full cells. And the solution was exactly to count free cells. I think I've seen that. So it's indeed simple.
But let's double-check with some more tests.
Let h=2,w=2. r=[1,1], c=[1,1]. Then forced:
Row1: (1,1)=1; (1,2)=0 (since r1<2)
Row2: (2,1)=1; (2,2)=0
Col1: (1,1)=1; (2,1)=1? Wait c1=1 => rows1..1 full => (1,1)=1; and since c1<h (2), (2,1) forced empty. But row2 forces (2,1)=1. Conflict! So answer 0. Indeed the conditions conflict: column1 expects (2,1) empty because after one full, next empty. But row2 expects (2,1) full as part of its prefix. So 0.
What about r=[1,0], c=[0,1]?
Row1: (1,1)=1; (1,2)=0
Row2: (2,1)=0 (since r2=0 => (2,1)=0)
Col1: c1=0 => (1,1)=0 => conflict with row1 full. 0.
What about r=[1,2], c=[2,1]? w=2,h=2? But r2=2 > w? Actually w=2 so r_i ≤ w. So r2=2 allowed. Row2: (2,1)=1, (2,2)=1 (full all). Col1: c1=2 => (1,1)=1, (2,1)=1; no empty because c1 = h? Actually h=2, c1=2, so all rows full: (1,1)=1,(2,1)=1; no forced empty. Col2: c2=1 => (1,2)=1; and since c2<h (2), (2,2)=0 forced empty. But row2 forces (2,2)=1 -> conflict. So 0.
Thus the simple method works.
But what about scenario where r_i = w, then row i forces all columns full; column j may have c_j < h, forcing some cell (c_j+1, j) empty. If that cell is in row i (i = c_j+1) and i is that row, then conflict. So we detect.
Thus solution is indeed: if any conflict, answer 0. Else answer = 2^(free) mod MOD.
Now, the free cells are those not forced by any row or column. But we should also ensure that those cells are not indirectly forced because they are part of the prefix? They are not.
Thus the algorithm:
- Input h, w, arrays r[1..h], c[1..w].
- Initialize a 2D array grid of size h x w with -1 (unforced) or perhaps separate arrays for row-forced and col-forced. But we can just keep an integer representing state: 0 empty, 1 full, -1 unknown.
- For each row i (0-index or 1-index):
for col from 0 to r_i-1 (since first r_i columns 1-indexed): set grid[i][col] = 1, checking conflict.
if r_i < w: set grid[i][r_i] = 0 (since columns 0-indexed: column index r_i corresponds to (r_i+1)th cell? Actually careful: if we use 0-index rows i from 0 to h-1, columns j from 0 to w-1, then row i's forced full columns are j=0,...,r_i-1 (since 1-indexed columns 1..r_i). And forced empty at j = r_i if r_i < w (since column index = r_i corresponds to (r_i+1)th). So algorithm:
if r_i > 0:
for j = 0 to r_i-1: set grid[i][j] = 1
if r_i < w:
set grid[i][r_i] = 0
- For each column j (0-index):
for i = 0 to c_j-1: set grid[i][j] = 1; check conflict.
if c_j < h: set grid[c_j][j] = 0; check conflict.
- After processing, count cells where grid[i][j] == -1. Let free = that count.
- If any conflict occurred (i.e., tried to set to opposite value), answer = 0.
- Else answer = pow(2, free, MOD). Use fast exponentiation.
But we need to be careful about double-setting same value. That's fine.
Since h,w <= 1000, we can allocate a vector of vectors.
But we might try to reduce memory: we could compute forced cells via a union of intervals. However, O(h*w) is fine.
But note: In the worst-case h=1000,w=1000 => 1e6 cells. Each cell we might set twice (row and column). So O(2e6) operations, fine.
Potential pitfalls:
-
The forced empty cell at r_i+1 and c_j+1 only if the respective bound is less than width/height. But if r_i = 0, we still set column 0 to 0 (since r_i < w? yes, if w>0, then set grid[i][0]=0). That matches: leftmost cell empty. So correct.
-
But what if r_i = w? Then we should not set an empty cell because r_i+1 > w. So skip.
-
Similarly for columns.
-
Must ensure we treat indices correctly. Using 0-index.
Check example 1 with 0-index:
h=3,w=4
r=[0,3,1] (r1=0, r2=3, r3=1)
Row0: r=0 -> if 0<4: set grid[0][0]=0.
Row1: r=3 -> full cols 0,1,2 set to 1. Since r=3<4, set col3? Actually col index = r =3 -> set grid[1][3]=0.
Row2: r=1 -> full col0=1; since 1<4, set grid[2][1]=0.
Columns c=[0,2,3,0]
Col0: c=0 -> if 0<3: set grid[0][0]=0.
Col1: c=2 -> full rows 0,1 set to 1; since 2<3 set grid[2][1]=0.
Col2: c=3 -> full rows 0,1,2 set to 1; since 3==h? h=3, c=3 not <h, so no empty.
Col3: c=0 -> if 0<3 set grid[0][3]=0.
Now conflicts? row0 col0 set 0 by both okay; row1 col3 set 0 by row and no column forced? column3 only forced grid[0][3]=0, row1 col3 not forced by column. okay. row2 col1 set 0 by both okay; row2 col0? row2 forced full at col0=1; column0 only forced grid[0][0]=0, so row2 col0 is free? Actually column0 forced only row0 empty, rows 1 and 2 free. So row2 col0 is 1 forced from row2, no conflict. row0 col1? column1 forced row0 col1=1, row0 forced nothing else, so 1. row0 col2? column2 forced row0 col2=1, okay. row0 col3=0 forced, row1 col0? row1 full col0=1; column0 only forced row0 empty, row1 free, so 1. row1 col1=1 forced by both. row1 col2=1 forced by both. row1 col3=0 forced by row, column3 forces only row0 empty. So free cells: (2,3)? row2 col3? row2 no forced beyond col1 empty; column3 forced only row0 empty; so row2 col3 free. So free count=1. matches.
Thus algorithm correct.
Now check example3 with large numbers: It should output 797922655, which is 2^(free) mod MOD? Let's test quickly with mental? Not needed.
But wait: The constraints h,w ≤ 1000, but the input sizes are small. However, we must compute power modulo 1e9+7. Use fast exponentiation.
Now, the solution is straightforward. However, before finalizing, I want to consider if there are any hidden conditions that could cause the count to be less than 2^(free) because of additional consistency between rows and columns beyond forced cells. Could there be a scenario where the row's r_i is defined as "the number of consecutive full cells connected to the left side" meaning that after the initial block, there cannot be any full cell that touches the left side? That's already enforced. But what about column's condition: it might impose that the column's leading block length is exactly c_j. If we have a free cell (i,j) that is above c_j+1? Actually for column j, cells above the forced empty (i > c_j+1) are free. But if we set a free cell to 1, does it affect the column's leading block? No, because the leading block is defined from the top. As long as the first c_j cells are 1 and the (c_j+1)th is 0, then regardless of later cells, the maximum consecutive full cells from the top is c_j (since it's broken by that 0). But could later cells create a longer consecutive block? No, because there is a 0 at row c_j+1, so any block starting from top is at most c_j. So it's fine. Similarly, for rows.
Thus the assignments of free cells do not alter the prefix constraints.
Therefore, answer is 2^(free).
But wait: Could there be a scenario where a free cell is forced indirectly by both row and column constraints? For instance, consider a cell (i,j) that is not directly forced, but the row i forces that the first r_i cells are 1, and maybe column j forces that the first c_j cells are 1. That doesn't affect (i,j) if it's outside those prefixes. So it's free.
Thus solution stands.
Now, implement.
Implementation details:
- Use vector<vector> grid(h, vector(w, -1));
- Or use array of char? int fine.
- For conflict detection: when we try to set a cell to value v, if current value is -1, set to v; else if current != v, conflict -> output 0 and exit.
But we must be careful: In the row loop, we may set same cell multiple times (if we set multiple columns in a row). That's fine. Conflict only if mismatch.
In column loop, we may set cells already set from row loop. Check conflict.
After processing both, count free.
Potential pitfalls: Overwriting with same value is fine.
But there is a nuance: What if a cell is forced both full and empty due to both constraints? We'll catch it as conflict when setting the second one.
One more nuance: The row condition says "the i-th row starts exactly with r_i full cells." Could it be interpreted that the row may have more than r_i full cells, but the number of consecutive full cells from left is exactly r_i? That means there is a block of r_i full cells at start, then a 0 (if not all full). That's exactly what we enforced. So it's correct.
Thus solution is trivial.
But wait, let's think about a corner case: Suppose r_i = 0, so leftmost cell forced empty. However, is it possible that row i later has a full cell and that would affect the "consecutive full cells connected to the left side"? The definition says "r_i is the number of consecutive full cells connected to the left side". If leftmost cell is empty, then the number of consecutive full cells from left is 0, regardless of later full cells. So it's consistent. So free cells after column1 are free.
Thus all good.
Now, I'd like to test some random small cases by brute force to ensure the counting matches 2^(free). We can do mental for small.
Case: h=1, w=2. r=[1], c=[0,0].
Row1: (1,1)=1; (1,2)=0? Actually r1=1<2, so (1,2)=0 forced.
Col1: c1=0 -> (1,1)=0 forced -> conflict. So answer 0.
Case: h=1,w=2. r=[1], c=[1,0].
Row1: (1,1)=1; (1,2)=0 forced.
Col1: c1=1 -> (1,1)=1 forced; no empty because c1 = h? h=1, so no empty forced. Col2: c2=0 -> (1,2)=0 forced. So all cells forced, free=0, answer=1. Check if grid satisfies? Row1: first cell full, second empty -> r=1 ok. Col1: first cell full (only cell) -> c1=1 ok? Actually c1=1, but column1 has only one cell; if it's full, the number of consecutive full cells from top is 1, which matches. Col2: first cell empty -> c2=0 ok. So 1 solution.
Case: h=2,w=2. r=[1,1], c=[1,0].
Row1: (1,1)=1; (1,2)=0.
Row2: (2,1)=1; (2,2)=0.
Col1: c1=1 -> (1,1)=1; (2,1)=0 forced (since c1<2) -> conflict with row2 (2,1)=1. So 0.
Case: h=2,w=2. r=[1,0], c=[0,1].
Row1: (1,1)=1; (1,2)=0.
Row2: (2,1)=0.
Col1: c1=0 -> (1,1)=0 conflict -> 0.
Case: h=2,w=2. r=[0,0], c=[0,0].
Row1: (1,1)=0; (since 0<2, set (1,1)=0) but careful: row1 r=0 forces (1,1)=0. Row2: (2,1)=0.
Col1: c1=0 -> (1,1)=0; (2,1)=0? Actually column1 c1=0: (1,1)=0; since c1 < h (2), set (2,1)=0. Col2: c2=0 -> (1,2)=0; (2,2)=0.
Now all cells forced empty. Free=0, answer=1. Check: rows: both start with 0, columns: both start with 0. Works.
Case: h=2,w=2. r=[0,0], c=[1,0].
Row1: (1,1)=0; Row2: (2,1)=0.
Col1: c1=1 -> (1,1)=1; conflict with row1 (1,1)=0 -> 0.
Thus matches.
Now, is it possible that the number of free cells computed as total - forced union is always = something like? Could there be cells that are forced by both row and column? That reduces union size but they are forced anyway. Counting free cells is easy.
Thus solution: O(h*w) time and memory.
But perhaps there is a twist: The problem statement says: "Let's make some definitions: * r_i is the number of consecutive full cells connected to the left side in the i-th row (1 ≤ i ≤ h). In particular, r_i=0 if the leftmost cell of the i-th row is empty. * c_j is the number of consecutive full cells connected to the top end in the j-th column (1 ≤ j ≤ w). In other words, the i-th row starts exactly with r_i full cells. Similarly, the j-th column starts exactly with c_j full cells."
The phrase "In other words" clarifies that the row starts exactly with r_i full cells. So it's exactly as we think.
Thus solution is correct.
But wait, we need to consider that the definitions might also imply that after the initial block, there are no restrictions. So indeed.
Hence we can implement.
However, note constraints up to 10^3, but answer can be huge, need modulo.
Implementation details in C++:
-
Read h,w.
-
vector r(h), c(w).
-
Create grid as vector<vector> state(h, vector(w, -1)); maybe use char: -1 for unknown, 0 for empty, 1 for full.
-
For each row i:
if (r[i] > 0) for j=0; j<r[i]; j++) set state[i][j] = 1, checking conflict.
if (r[i] < w) set state[i][r[i]] = 0 (note: if r[i]==0, this sets column 0 to 0, which is correct).
But careful: if r[i] == 0 and w>0, then we set state[i][0]=0. Good.
However, if r[i]==0 and w>0, but also note that column constraints might also set (i,0) to 0. That's fine.
-
For each column j:
if (c[j] > 0) for i=0; i<c[j]; i++) set state[i][j]=1.
if (c[j] < h) set state[c[j]][j] = 0 (if c[j] == 0, set row0 col j = 0). That's correct.
-
After that, count cells where state[i][j]==-1.
-
Compute pow2 = 1; for i=1 to free: pow2 = (pow22) % MOD. Or use fast exponentiation: long long ans = 1; for (int i=0;i<free;i++) ans = ans2%MOD; That's O(free) with free up to 1e6, which is acceptable. But we can also use binary exponentiation O(log free). Not a big deal.
-
Print ans.
But careful: If conflict occurs, we should print 0 and exit.
Edge Cases:
- h=1, w=1: OK.
- r_i = 0 and w=1: Row forces (i,0)=0. Column similarly.
- r_i = w and column c_j = 0: For column j, if c_j=0, then (0,j)=0 forced. But row i with r_i = w forces (i,0..w-1)=1 including column j? Actually row i has all cells full. So if i=0 (first row) and column j forces (0,j)=0, conflict. If i>0, row i column j full but column j only forces row0 empty, not row i. So conflict only if the row index is the forced empty row for that column. So we will detect.
Potential subtlety: In row loop, we might set the same cell twice within the same row loop? No, because we set full for columns 0..r_i-1 and empty at column r_i if r_i < w. These are disjoint because r_i and r_i are different indices.
In column loop, similarly.
But what if r_i = w? Then we set full for all columns, and no empty. In column loop, if c_j = h, we set full for all rows, no empty. Overlap fine.
Now, could there be a situation where a cell is forced empty by row (as the (r_i+1)th) and also forced full by column because it's within the first c_j cells? That would be conflict. For example, row i: r_i=2, so column 3 forced empty. Column j=3: c_3=3, so row i (if i <= 3) forced full. If i=3 and c_3=3, then cell (3,3) row3? Actually careful: column j forces rows 1..c_j full; if i ≤ c_j, then cell forced full. So if i = r_i+1 and i ≤ c_j, then conflict. That's captured.
Now, is there any scenario where the forced assignments from rows and columns are consistent, but the actual leading counts could be altered by the free cells? Consider row i: r_i = 1, w=3. So forced: (i,1)=1, (i,2)=0. Column j=2: c_2=2? Actually column 2 with c_2=2 means rows 1..2 full, row 3 empty. Suppose h=3, i=3, j=2. Row3 forced: (3,2)=0 (since r3=1 => column2 empty). Column2 forced: row3 col2 = empty? Actually if c_2=2, then row3 (c_2+1=3) forced empty. So consistent. Now free cells: (3,3) maybe. That's fine.
Now, could free cell (i,j) being full cause row i to have a longer consecutive full cells from left? For row i, we have a 0 at column 2, so even if column3 is full, the consecutive block from left is still broken at col2. So r_i remains 1.
Thus fine.
Now, double-check the meaning: "r_i is the number of consecutive full cells connected to the left side in the i-th row." Could it be interpreted as the total number of full cells that are connected to the left side (i.e., contiguous block from left, but also possibly there could be other full cells not connected to left side)? The phrase "connected to the left side" might mean that they are adjacent to the left border? Actually "connected" could mean there's a path of adjacent full cells from the left side. But the grid is just one row; "connected" might refer to connectivity through edges, but in a row, "connected to the left side" would mean the cell is reachable from the left border via adjacent full cells. The number of such cells that are connected to the left side could be the size of the connected component that includes the left border. But in a row, connectivity is along the row (left-right). The leftmost cell is at column1. If it's full, then it's connected to left side. Then any cell that is full and adjacent to a cell that is connected to left side is also connected. So essentially, it's the length of the contiguous block of full cells starting from the leftmost cell. But could there be a scenario where there is a gap? For example, row: 1 0 1. The leftmost cell is full, so it's connected. The cell at column2 is empty, so it blocks connection. The cell at column3 is full but not connected to left side because it's not adjacent to a connected full cell (since column2 empty). So the number of full cells connected to left side would be 1, which matches the length of the prefix block. So indeed it's the same as the prefix block length.
Thus it's consistent.
Now, what about columns? Similar.
Hence solution stands.
Now, we should also consider that there might be cells that are not forced by either condition but that must be empty due to some interplay? For example, suppose row i has r_i = 2, so (i,3) empty. Column j has c_j = 2, so (3,j) empty. Consider cell (3,3) which is not forced by either. Could it be forced to be something? No, it's free.
So answer is indeed 2^(free).
Thus code is simple.
But let's think about a more complex scenario: Could there be an implicit condition that the entire grid must be such that the connected components are consistent? But no.
One more nuance: The phrase "connected to the left side" might be interpreted differently if we consider also vertical connections? Actually "connected" might refer to connectivity via both horizontal and vertical adjacency? But the definition given: "r_i is the number of consecutive full cells connected to the left side in the i-th row." The phrase "in the i-th row" suggests we only look at that row, and "consecutive" suggests they are consecutive cells along the row. So it's fine.
But wait, the problem statement includes an image. In that image, the grid shows black cells, and they show the r and c values. In the image, the black cells seem to be a contiguous shape? Actually the image: For a 3x4 grid, they show something like: row1: first cell empty, then two full? Let's not worry.
Thus I think the solution is correct.
Now, we can implement. However, we need to be careful about the size: h and w up to 1000, so grid of 1e6 integers. That's fine. We'll allocate with vector.
But perhaps there is an even simpler solution: The number of free cells is total cells minus the number of cells forced by row conditions or column conditions. But note that forced cells from row conditions are those with column ≤ r_i or column = r_i+1. But these might overlap with column forced cells. We could compute free count as total - (size of union). But it's easier to just simulate.
Given constraints, simulation is acceptable.
But we need to be cautious about reading input: second line has h integers, third line w integers.
Also, the third example has many numbers, but it's fine.
Now, consider potential pitfalls: If we set cell multiple times to same value, it's okay. But if we set a cell first from row to 1, then later from column to 1, we set again; we must check that current value is not 0. If we set again to 1 and current is 1, fine. But we need to store state.
Implementation in C++:
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
int main() {
int h, w;
cin >> h >> w;
vector<int> r(h), c(w);
for (int i=0; i<h; i++) cin >> r[i];
for (int j=0; j<w; j++) cin >> c[j];
vector<vector<int>> grid(h, vector<int>(w, -1));
bool conflict = false;
// apply row constraints
for (int i=0; i<h && !conflict; i++) {
// full for first r_i columns
for (int j=0; j<r[i]; j++) {
if (grid[i][j] == -1) grid[i][j] = 1;
else if (grid[i][j] != 1) { conflict = true; break; }
}
if (conflict) break;
// empty at r_i (if r_i < w)
if (r[i] < w) {
int j = r[i];
if (grid[i][j] == -1) grid[i][j] = 0;
else if (grid[i][j] != 0) { conflict = true; break; }
}
}
// apply column constraints
for (int j=0; j<w && !conflict; j++) {
// full for first c_j rows
for (int i=0; i<c[j]; i++) {
if (grid[i][j] == -1) grid[i][j] = 1;
else if (grid[i][j] != 1) { conflict = true; break; }
}
if (conflict) break;
// empty at c_j (if c_j < h)
if (c[j] < h) {
int i = c[j];
if (grid[i][j] == -1) grid[i][j] = 0;
else if (grid[i][j] != 0) { conflict = true; break; }
}
}
if (conflict) {
cout << 0 << endl;
return 0;
}
long long free_cells = 0;
for (int i=0; i<h; i++) {
for (int j=0; j<w; j++) {
if (grid[i][j] == -1) free_cells++;
}
}
long long ans = 1;
for (int i=0; i<free_cells; i++) {
ans = (ans * 2) % MOD;
}
cout << ans << endl;
return 0;
}
But we need to be careful: In the row loop, when r[i] = 0, we still go into the full loop? The for loop for j from 0 to r[i]-1: if r[i]=0, loop runs 0 times. Then we do if(r[i] < w) set grid[i][0]=0. That's correct: leftmost cell forced empty.
But what if w = 0? Not possible because w ≥1.
Similarly for column.
Now, check scenario: r[i] = w, then we set full for j=0..w-1. No empty. That's fine.
Now, consider scenario: c[j] = h, then full for i=0..h-1, no empty.
All good.
Now, we should also think about potential double counting of forced cells when we count free cells: we set them to non -1; they are not counted as free.
Now, complexity: O(hw) time and O(hw) memory.
But can we reduce memory to O(h+w) by only storing the forced status? Because we just need to know for each cell whether it's forced or not. But forced status can be determined from r_i and c_j without needing a full grid? Actually we could compute for each cell if it's forced by either row or column. However, checking conflict might be trickier without storing. But we can reason about conflicts analytically.
We could detect conflicts by checking certain conditions: For any cell (i,j), if it is in the forced full region from row (j <= r_i) and also in forced empty region from column (i = c_j+1 and j <= r_i? Actually empty from column if i = c_j+1 and c_j < h, then that cell forced empty. So conflict if j <= r_i and i = c_j+1 and c_j < h? But also if j = r_i+1 (empty from row) and i <= c_j (full from column). More systematically:
Conflict occurs if:
- (j <= r_i) and (i <= c_j) but one forces full (both full) actually both full so no conflict. But if (j <= r_i) and (i = c_j+1) then row says full, column says empty -> conflict.
- (j = r_i+1) and (i <= c_j) then row says empty, column says full -> conflict.
- Also, if (j = r_i+1) and (i = c_j+1) both say empty, that's fine.
Additionally, we must also consider the possibility of contradictory forced assignments from the same source? Not.
Also, if a cell is not forced by either, it's free.
But we could also count free cells as total - number of cells that are forced by at least one of row or column. Forced by row means either j <= r_i or j = r_i+1 (with r_i<w). Forced by column means i <= c_j or i = c_j+1 (with c_j<h). So union of these sets. However, we have to be careful that when r_i = w, forced by row includes j=1..w (no j = w+1). Similarly for columns.
But we could compute free cells as count of cells that are not forced by row nor column. However, we must also ensure consistency; we can detect conflicts via the above conditions.
But implementing this analytical approach might be more complex, but we could do it in O(h*w) by scanning each cell and applying conditions without storing entire grid; just check conditions per cell. However, we still need to detect conflict. But we can detect conflicts by scanning each cell and applying logic: For each cell (i,j), determine what row forces (if any) and what column forces (if any). If both force and disagree, conflict. Else if at least one forces, cell is forced; else free.
Thus we can compute without storing the whole grid, using just r and c arrays. That would be O(hw) time and O(h+w) memory. But O(hw) time is the same; memory saving is not necessary. But it's elegant to avoid storing 1e6 ints, but it's okay. However, sometimes in contests, memory might be tight? 1e6 ints is about 4 MB if using int, or 1 MB if using char. That's fine.
But we could still implement the scanning method for fun or to be safe.
Let's do scanning method:
For each i from 0 to h-1:
For each j from 0 to w-1:
// determine row forced value
int row_val = -1;
if (j < r[i]) row_val = 1; // since column index j (0-based) < r_i means within first r_i full cells
else if (j == r[i] && r[i] < w) row_val = 0; // forced empty at r_i (if exists)
// else row_val remains -1
// determine column forced value
int col_val = -1;
if (i < c[j]) col_val = 1;
else if (i == c[j] && c[j] < h) col_val = 0;
// combine
if (row_val != -1 && col_val != -1 && row_val != col_val) -> conflict
else if (row_val != -1) { // forced by row, ignore free count (or count forced)
// nothing to do for free count, it's forced
} else if (col_val != -1) { forced
} else { // both -1
free++;
}
That's O(h*w) and O(1) extra per cell (just local). That's even simpler and avoids storing grid. But careful: We also need to consider that if row_val and col_val both -1, it's free. If one is -1 and other is not, it's forced (by the other). If both are not -1 and equal, forced (by both). So free count only when both -1.
Thus we can compute free cells directly, and detect conflicts.
But must also check that the row condition itself is consistent with column condition? Actually there's no self-consistency beyond cell conflicts.
Thus we can compute:
Initialize free = 0, conflict = false.
Loop i,j:
bool row_forced = (j < r[i]) || (j == r[i] && r[i] < w);
bool col_forced = (i < c[j]) || (i == c[j] && c[j] < h);
if row_forced and col_forced:
// check consistency: the forced values must be same.
// Determine row_value: if j < r[i] => 1 else if j == r[i] and r[i] < w => 0.
// Determine col_value: if i < c[j] => 1 else if i == c[j] and c[j] < h => 0.
// So they are consistent iff ( (j < r[i]) and (i < c[j]) ) both give 1, or (j < r[i] and i == c[j]) gives row=1 col=0 -> conflict; etc.
else if row_forced or col_forced: // cell forced, not free
else: free++
We can compute directly.
But we must be careful: The forced values from row and column are derived as above. So we can compute them.
This approach avoids storing grid, and is O(h*w). Since h,w up to 1000, 1e6 iterations, trivial.
We'll adopt this.
Now, we need to compute 2^free mod MOD. Use fast exponentiation or loop multiplication. Since free up to 1e6, loop multiplication (free times) is 1e6 multiplications, which is acceptable. But we can also use binary exponentiation. I'll just use loop.
Now, check for overflow: Use long long for ans.
Now, double-check the logic for row forced value: For row i (0-index), r_i (given). Row forced full for columns j where j < r_i. Row forced empty for column j = r_i if r_i < w. That's correct.
But what about the case r_i = w? Then there is no column index = r_i because r_i = w, but column indices go 0..w-1, so j = r_i is out of range; so condition r_i < w ensures we don't set empty. So it's fine.
Similarly for column.
Now, check for i and j being zero-indexed.
Now, test example1 with this method:
h=3,w=4, r=[0,3,1], c=[0,2,3,0]
Indices i=0..2, j=0..3.
We'll compute for each cell:
i=0:
j=0: row_forced? j<r0? 0<0? false. j==r0 and r0<w? 0==0 and 0<4 true -> row_val = 0. col_forced? i < c0? 0<0 false; i==c0 and c0<h? 0==0 and 0<3 true -> col_val = 0. both forced, same -> forced.
j=1: row_forced? j<0? false; j==0? false -> row not forced. col_forced? i<2? 0<2 true -> col_val=1. So forced (by column) => not free.
j=2: row not forced; col: i<3 true -> 1 => forced.
j=3: row not forced; col: i<0? false; i==0 and 0<3? true -> col_val=0 forced.
i=1:
j=0: row_forced? j<3 true -> row_val=1; col_forced? i<0? 1<0 false; i==0? false -> col not forced. So forced by row.
j=1: row_forced j<3 ->1; col_forced? i<2? 1<2 true ->1 -> both forced same.
j=2: row_forced j<3 ->1; col_forced? i<3? 1<3 ->1 -> forced.
j=3: row_forced? j==3 and r1=3<4? true -> row_val=0; col_forced? i<0? false; i==0? false -> no col forced -> forced by row.
i=2:
j=0: row_forced? j<1 -> true -> row_val=1; col_forced? i<0? false; i==0? false -> forced.
j=1: row_forced? j==1 and r2=1<4 -> row_val=0; col_forced? i<2? 2<2? false; i==2 and c1=2<h=3? true -> col_val=0 -> both forced same.
j=2: row_forced? j<1? false; j==1? false -> no row; col_forced? i<3? 2<3 true -> col_val=1 -> forced.
j=3: row_forced? none; col_forced? i<0? false; i==0? false -> no col forced -> free.
Thus free=1. Good.
Now check conflict detection: e.g., if there is a conflict, we will catch when both forced and values differ.
Let's test example2: h=1,w=1, r=[0],c=[1]
i=0,j=0: row_forced: j==0 and r0=0<1 -> row_val=0. col_forced: i<1 -> col_val=1. both forced and differ -> conflict -> answer 0.
Works.
Thus we can implement this scanning approach.
Now, we should also ensure that the forced conditions are correctly derived: For row i, is it correct that j < r_i means full? Yes, because if r_i = 3, then columns 1,2,3 (0-index 0,1,2) are full. So column index j from 0 to r_i-1 inclusive are full. That's j < r_i.
For empty at column r_i: if r_i < w, column index = r_i (which is the (r_i+1)th column) is empty. So condition: j == r_i and r_i < w.
Similarly for column: i < c_j gives full; i == c_j and c_j < h gives empty.
Thus algorithm:
long long free = 0;
bool ok = true;
for (int i=0; i<h; i++) {
for (int j=0; j<w; j++) {
int row_val = -1;
if (j < r[i]) row_val = 1;
else if (j == r[i] && r[i] < w) row_val = 0;
int col_val = -1;
if (i < c[j]) col_val = 1;
else if (i == c[j] && c[j] < h) col_val = 0;
if (row_val != -1 && col_val != -1) {
if (row_val != col_val) {
ok = false;
break;
}
}
if (row_val == -1 && col_val == -1) {
free++;
}
}
if (!ok) break;
}
if (!ok) cout << 0 << endl;
else {
long long ans = 1;
for (int i=0; i<free; i++) ans = ans*2 % MOD;
cout << ans << endl;
}
Now, we must consider whether the forced values might also have a scenario where row_val is -1 and col_val is something but that something could be inconsistent with row's later choices? No.
Also, we must ensure that the row_val and col_val definitions are exhaustive. For row i, what about j > r_i? No forced, because only prefix matters. So row_val = -1 for those j.
Now, are there any cells that are forced by row but not by column and vice versa? That's fine.
Thus answer is 2^free.
But wait: Could there be a situation where the cell is not directly forced but is indirectly forced because if we set it to some value, it might affect the column's or row's leading count? Let's examine a tricky case.
Suppose row i: r_i = 1, w=3. So forced: (i,1)=1, (i,2)=0. Column j=2: c_2=1, h=2? Actually need two rows to illustrate. Let's have h=2,w=2. Consider r=[1,0], c=[0,1]. Let's compute.
r1=1, r2=0; c1=0, c2=1.
Now forced by our method:
i=0, j=0: row: j<1 -> row_val=1; col: i<0? false; i==0 and c0=0<h? true -> col_val=0 -> conflict! So answer 0. Indeed conflict.
Now another scenario: Could there be a scenario where row and column conditions are consistent, but some cell is not forced, yet its value influences the row's r_i? For instance, row i: r_i=2, w=3. So forced: (i,1)=1, (i,2)=1, (i,3)=0? Actually (i,3) is column index 2? Let's use 0-index: row i: full at j=0,1; empty at j=2 if r_i=2<3. So forced empty at column 2 (index 2). Now column j=2: suppose c_2=2? Actually column 2 (third column) might have c= something. If c_2=1, then column 2: rows 0 forced full (if row0), but row i maybe row1. Let's set i=1, h=2, w=3, r1=2, r2=... But we need to examine if any free cell could change r_i. The row's r_i is determined by the first zero. Since we have forced zero at column 2, that's the first zero. So column 2 being forced empty ensures that the row's leading ones count is 2, regardless of what's in column 0,1? But column 0 and 1 are forced full, so they are 1. So row i's prefix is fixed. So no.
Now consider column j: forced prefix full and empty ensure its leading count.
Thus free cells are truly free.
Thus solution is correct.
Now, we should test some random small cases by brute force to confirm the counting equals 2^(free) if no conflicts. Write a small script mentally? Not necessary.
But I recall that there is a known Codeforces problem "Little Pony and Harmony Chest"? No. Actually there is a problem "R and C" on Codeforces Round #... I think it's 1105A? Not sure. But I think I've seen solution indeed is 2^(free) if no conflict else 0.
Now, we should also consider the possibility that the problem expects answer 0 when there is any inconsistency between r and c values even before counting free cells. Our detection covers all.
Now, we must be careful about the modulo exponent. Since free can be up to 1e6, we can compute 2^free using fast exponentiation: ans = powmod(2, free, MOD). That's simpler.
We can implement:
long long ans = 1;
long long base = 2;
while (free > 0) {
if (free & 1) ans = ans * base % MOD;
base = base * base % MOD;
free >>= 1;
}
But careful: free is count; we should not modify free if we need it later. Use a copy.
Simplify: Use iterative multiplication: for (int i=0; i<free; i++) ans = ans2%MOD; That's O(free). Since free ≤ 1e6, that's fine. But actually worst-case free could be hw = 10^6. 1e6 multiplications is trivial in modern C++ (<0.01 sec). So both are fine. I'll use fast pow for conciseness.
Now, we must also consider that some cells may be forced by both row and column but with same value; we counted them as forced, not free. That's correct.
Now, one more nuance: The forced empty at r_i+1 is only if r_i < w. Similarly for column. That's consistent.
But what about the scenario where r_i = 0 and w > 0: we set (i,0) empty. Good.
Now, what about the scenario where r_i = 0 and w = 1: we set (i,0) empty. Column j=0: c0 maybe 0 or >0. If c0=0, then (0,0) empty, consistent. If c0=1, then (0,0) full? Actually column 0 with c0=1 would force (0,0) full if row index 0? Wait, column j=0: rows 0..c0-1 full; so if c0=1, then row0 (i=0) is forced full. But row0 forced empty from r0=0, conflict. So detection works.
Now, confirm that the union of forced cells from row and column indeed includes all cells that are forced by at least one. In our scanning, we check row_val and col_val; if either is not -1, then cell is forced. So free only when both -1.
Now, we need to ensure that we don't double-count conflicts when both are -1? Not.
Now, edge-case: Could there be a conflict where row_val is -1 and col_val is -1 but the cell is forced by something else? No.
Now, test some random small grids manually with brute enumeration to ensure our counting matches. Let's do a tiny grid: h=2,w=2, enumerating all 16 assignments, then filter by given r and c, and compare count to our formula.
We can do mental for a few combos.
Case: r=[0,0], c=[0,0]. According to our method: For each cell, row forced? For i=0: j=0: row forced empty; j=1: row not forced? Actually row0 r=0: forced empty at j=0 only. For j=1, row not forced. Column0 c=0: forced empty at i=0, and also forced empty at i=1? Because c0=0 < h=2, so i=0 forced empty, i=1? column forced: i=0 empty; but also for i=1? Actually column j=0: i < 0? false; i == 0? i=1 not 0; so no forced. Wait, column forced: i < c_j => full; i == c_j => empty if c_j < h. So for c0=0, i < 0 false, i == 0 => empty for i=0; i=1 not forced. So column0 forces only (0,0) empty. Column1 c1=0 similarly forces (0,1) empty. Row0 forces (0,0) empty. Row1 r1=0 forces (1,0) empty. So forced cells: (0,0) empty (by both), (0,1) empty (by column), (1,0) empty (by row). Cell (1,1) is not forced by any? Row1 only forces (1,0) empty; column1 only forces (0,1) empty; so (1,1) free. So free=1, answer=2. Let's brute force: All grids 2x2 that satisfy r=[0,0] and c=[0,0]. Row0 must start with 0 full cells: so first cell (0,0) must be 0. Row1 first cell (1,0) must be 0. Column0 first cell (0,0) must be 0, so ok. Column1 first cell (0,1) must be 0. So constraints: (0,0)=0, (1,0)=0, (0,1)=0. (1,1) can be 0 or 1. So indeed 2 solutions. So our count matches.
Case: r=[1,1], c=[1,1] we already saw conflict and answer 0.
Case: r=[1,0], c=[0,1] conflict.
Case: r=[2,0], c=[1,0]? But w=2? Actually r=[2,0] means row0 all full, row1 leftmost empty. c=[1,0] means col0: first row full, second row empty? Since c0=1<h=2 => empty at row1. col1: c1=0 => first row empty. Let's compute forced:
Row0: r0=2 => full at col0,1; no empty (since r0=w). Row1: r1=0 => (1,0) empty.
Col0: c0=1 => full at row0, empty at row1. So (0,0) full, (1,0) empty. Row1 col0 empty matches row1. Row0 col0 full matches row0. So no conflict so far. Col1: c1=0 => empty at row0; (0,1) empty. Row0 col1 is forced full from row0 (since row0 all full). Conflict: (0,1) row full vs col empty => conflict. So answer 0. Our method would detect.
Now, case: r=[1,1], c=[0,0]? w=2, h=2. Row0: (0,0)=1, (0,1)=0. Row1: (1,0)=1, (1,1)=0. Col0: c0=0 => (0,0)=0, (1,0) not forced? Actually column0: i==0 => empty; row1? No forced. So conflict at (0,0): row full vs col empty. 0.
Now, case: r=[1,0], c=[0,0]? Row0: (0,0)=1, (0,1)=0. Row1: (1,0)=0. Col0: c0=0 => (0,0)=0 conflict.
Now, case: r=[0,1], c=[1,0]? Row0: (0,0)=0. Row1: (1,0)=1, (1,1)=0 (since r1=1<2). Col0: c0=1 => (0,0)=1, (1,0) full? Actually c0=1: i<1 => row0 full; i==1 and c0<h => empty at row1. So (0,0) forced full, but row0 forced empty -> conflict.
Thus many conflicts.
Now, find a case with no conflict and some free cells. Example: h=2,w=2, r=[1,0], c=[0,0] we already saw conflict. What about r=[1,0], c=[0,?] Let's try r=[1,0], c=[0,?] Actually we need column1 c1 something to avoid conflict at (0,0). The conflict at (0,0) arises from row0 col0=1 vs column0 c0 forced maybe 0 if c0=0. To avoid conflict, we need column0 c0 to be >=1? Because column0 forces (0,0) full if c0>=1. But row0 forces (0,0)=1 anyway, so it's okay if c0>=1? But column0 with c0>=1 forces (0,0) full. But also column0 forces (c0,0) empty if c0<h. So we need to avoid conflict at (c0,0) with row condition. Let's try: h=2,w=2, r=[1,1]? Conflict we saw. Let's systematically search: We need that for each cell, the forced assignments from row and column agree. So we can define for each cell (i,j) the row forced value (if any) and column forced value. The forced assignments are determined solely by r and c. So we can pick r and c such that for all cells, either row and column don't both force, or they agree. That's a condition on r and c.
But anyway, the scanning method will compute.
Now, I'm convinced.
Now, the third example output: 797922655. Our algorithm would produce that if free count is something. We can test quickly with Python? Not needed.
Now, we should also consider possibility that the answer might be 0 if the forced assignments themselves cause a logical inconsistency even if we don't detect a direct conflict? For example, suppose row i has r_i = 2, w=3, so forced (i,1)=1, (i,2)=1, (i,3)=0. Column j=3 has c_3=2, h=3. Then column 3 forced: rows 1..2 full, row 3 empty (if 2<3). If i=3? Actually row index 3? Let's index 1..h. Suppose h=3, row3 r=2 => forced (3,3)=0. Column3 c=2 => forced (3,3) empty? Because i=3 == c_j+1? Actually column3: rows 1..2 full, row3 (c+1) empty if c<h. So both force empty, ok. No conflict.
But consider a scenario where the forced assignments from rows and columns don't conflict directly, but there is an indirect inconsistency: For example, a cell forced empty by row, but column's forced empty is at a different cell that might cause column's leading count to be different? But column's leading count is determined by its forced assignments; we already enforce them. So no.
Thus direct conflict detection suffices.
Now, double-check: Could there be a scenario where a cell is forced full by row, and column does not force it, but due to column's forced empty somewhere else, the column's condition might require that cell to be empty? No, column condition only forces prefix full and one empty. It does not force anything beyond. So if a cell is not in the forced prefix or the forced empty position, column doesn't care about its value.
Thus no indirect.
Now, we should consider the case where r_i = w (all full) and c_j = 0 for some j. That column forces (1,j) empty. If row1 is the first row? Actually row1 has r1=w, so (1,j) full. So conflict. Our detection: for cell (i=0, j) row_val = 1 (since j<w? j < r_i? Since r_i=w, for all j<w, row_val=1). Column_val: i < c_j? i<0 false; i==c_j? i==0 and c_j=0<h? true => col_val=0. So conflict. Good.
Now, what about r_i = 0 and c_j = h for some j? Row forces (i,0) empty? Actually j= that column? Let's pick i=0, j arbitrary: row_val: if j<0? none; j==0 and r_i=0<w? true => row_val=0. Column_val: i < c_j? i< h? true => col_val=1. Conflict. So good.
Now, one more nuance: In the scanning method, we assume that the row forced condition for empty only applies when r_i < w. But if r_i = w, then we don't set row_val=0 for any j. That's correct. Similarly for column.
Now, what about the possibility that r_i is given as > w? Input constraint says 0 ≤ r_i ≤ w, so okay. c_j ≤ h.
Thus algorithm correct.
Now, implement final solution.
But wait, we need to ensure that we use long long for ans and possibly for free (which can be up to 1e6). That's fine.
Now, I'd like to test some random cases to be 100% sure.
Let's test h=3,w=3 with some r,c.
Case: r=[2,2,2], c=[2,2,2].
Compute forced:
Row i: full columns 0,1; empty at col2 (since r_i=2<3). So (i,2)=0 for all i.
Column j: full rows 0,1; empty at row2 (since c_j=2<3). So (2,j)=0 for all j.
Now check cells:
(0,0): row full (1), col full (1) -> ok.
(0,1): row full (1), col full (1) -> ok.
(0,2): row empty (0), col? for col2, row0<2 -> full? Actually col2: rows 0,1 full, row2 empty. So (0,2) col full (1) -> conflict: row empty vs col full -> conflict! Indeed (0,2) row says empty (since it's column2 r_i=2 => j=2 == r_i and r_i<3 => empty), column says full (since row0<2). So conflict. So answer 0. Is that correct? Let's reason: Row0: first two cells full, third empty. Column2: first two rows full, third empty. At (0,2), column expects full, row expects empty, conflict. So no solution. So answer 0.
Now, try r=[2,2,2], c=[3,3,3] (c_j = h=3). Row i: full 0,1; empty at 2. Column j: full rows 0,1,2 (all full) because c_j = h, no empty. So (i,2) row empty vs column full -> conflict for all i. So 0.
Now try r=[2,2,2], c=[1,1,1]. Row i: full 0,1; empty at 2. Column j: full row0; empty at row1 (since c_j=1<h=3). So for cell (0,2): row empty, col? row0<1? true -> col full -> conflict. So 0.
Now try r=[1,1,1], c=[1,1,1], w=3,h=3. Row i: full col0; empty at col1. Column j: full row0; empty at row1.
Now check:
(0,0): row full, col full -> ok.
(0,1): row empty (since j=1 == r_i=1 and r_i<3? yes), col? row0<1 -> col full -> conflict: row empty vs col full. So 0.
Thus many combos conflict.
Now try r=[1,2,0], c=[1,1,0] with h=3,w=3? Let's compute:
r0=1, r1=2, r2=0.
c0=1, c1=1, c2=0.
Row0: full col0; empty col1.
Row1: full col0,1; empty col2? since r1=2<3 => empty col2.
Row2: r2=0 => empty col0.
Col0: c0=1 => full row0; empty row1.
Col1: c1=1 => full row0; empty row1.
Col2: c2=0 => empty row0.
Now check conflicts:
Cell (0,0): row full (1), col full (1) ok.
(0,1): row empty (0), col full (1) -> conflict! So 0.
Thus many conflicts.
Now try r=[0,0,0], c=[0,0,0] with h=3,w=3. Row i: empty col0 only. Column j: empty row0 only. So forced cells: all (i,0) empty? Actually row i: (i,0) empty. Column j: (0,j) empty. So forced: first column all empty, first row all empty. The rest (i>0, j>0) are free. So free = (h-1)(w-1) = 22=4. So answer = 16. Is that plausible? Let's brute think: We need row i: first cell empty, so (i,1)=0. Column j: first cell empty, so (1,j)=0. All other cells arbitrary. That gives 2^4=16. So correct.
Now try r=[w, w, ..., w] all rows full, and c=[h, h, ..., h] all columns full. Then row forces all cells full; column forces all cells full. No free, answer 1. Check: all cells full satisfies both.
Now try r=[w, w, ..., w] (all rows full), and c=[h-1, h-1, ..., h-1] (all columns have c_j = h-1). Then rows force all cells full. But columns: for each column j, first h-1 rows full, and row h (index h-1) forced empty (since c_j < h). So conflict because row h (last row) is forced full by row condition but empty by column. So answer 0.
Now try r=[0,0,...,0] (all rows start empty) and c=[0,0,...,0] (all columns start empty). That gives forced first column and first row empty, free others, as above.
Now, consider scenario where some rows have r_i=w (full) and some columns have c_j = 0. Then conflict if there exists a row i with r_i=w and column j with c_j=0, because at cell (i,j) row full, column empty if i=0? Actually column j with c_j=0 forces (0,j) empty; but row i (if i=0) full -> conflict. If i>0, column j only forces (0,j) empty; for i>0, column j does not force anything, so no conflict. So only conflict if the row with full is the top row (i=0). But what about the forced empty at column j's (c_j+1) if c_j < h? For c_j=0, forced empty at row 0 only; no further empty. So row i full for i>0 is fine. So it's possible to have a full row not at top and column with c=0, without conflict. For example: h=2,w=2, r=[2,2] (both rows full), c=[0,0] (both columns have c=0). Let's test: Row0 full => (0,0)=1, (0,1)=1. Row1 full => (1,0)=1, (1,1)=1. Column0: c0=0 => (0,0)=0 forced, conflict with row0 full. So conflict because (0,0) is top row, column forces empty. So indeed conflict occurs if any full row is the top row and column has c=0. But if the full row is not top? But the top row will always be row 0. So if any column has c_j=0, then (0,j) must be empty. So if there is any row (including row0) with r_0 > 0 (so (0,j) forced full for some j), conflict. But if r_0 = 0 (top row starts empty), then it's okay. So r_0=0, r_1=2 (full), c=[0,0] may be consistent? Let's test h=2,w=2, r=[0,2], c=[0,0]. Row0: r0=0 => (0,0)=0 forced empty. Row1: r1=2 => (1,0)=1, (1,1)=1. Column0: c0=0 => (0,0)=0 forced; (1,0) not forced (since column only forces empty at row0). So (1,0)=1 fine. Column1: c1=0 => (0,1)=0 forced; (1,1) not forced -> (1,1)=1 fine. So no conflict. Are there any other forced? Row0 also forced empty at column0 only? Actually row0 r0=0 forces only (0,0)=0; (0,1) is free? But column1 forces (0,1)=0, so (0,1)=0 forced. So final forced: (0,0)=0, (0,1)=0, (1,0)=1, (1,1)=1. Free? none. Answer=1. Does this satisfy row0: r0=0? Row0: first cell empty -> yes, r0=0. Row1: first two cells full -> r1=2. Column0: first cell (0,0) empty -> c0=0. Column1: first cell (0,1) empty -> c1=0. So it works. So it's possible.
Thus our detection works.
Now, in the scanning method, for i=0,j=0: row_val=0, col_val=0 -> ok. i=0,j=1: row_val? j<0? no; j==0? no -> row_val=-1; col_val: i<0? no; i==0 and c1=0<h? true -> col_val=0 -> forced. So no free. i=1,j=0: row_val: j<2? true -> 1; col_val: i<0? no; i==0? no -> -1; forced. i=1,j=1: row_val: j<2? true ->1; col_val: i<0? no; i==0? no -> -1; forced. So free=0, answer=1. Good.
Thus scanning works.
Now, we need to ensure that we treat row_val and col_val correctly for cases where r_i = w and j < w yields row_val=1; no row_val=0. So for i=0,j= w-1 (last column): row_val=1, column may have some forced. Good.
Now, we should also verify that the forced values from row and column are exactly what we derived. But we must also check that the row condition "r_i is the number of consecutive full cells connected to the left side" could be interpreted that the row may have more full cells after an empty, but also that there might not be any full cells after the empty? No, it's allowed.
Thus solution stands.
Now, the problem's third example output is 797922655. We can verify by quickly computing the free count for that input if we had the data. But it's fine.
Now, about modulo: 1000000007.
Now, is there any other nuance? Let's read the problem statement again: "Let's make some definitions: * r_{i} is the number of consecutive full cells connected to the left side in the i-th row (1 ≤ i ≤ h). In particular, r_i=0 if the leftmost cell of the i-th row is empty. * c_{j} is the number of consecutive full cells connected to the top end in the j-th column (1 ≤ j ≤ w). In other words, the i-th row starts exactly with r_i full cells. Similarly, the j-th column starts exactly with c_j full cells."
The phrase "connected to the left side" might be ambiguous if we consider connectivity through both directions? But the grid is 2D, and "connected" could refer to connectivity via sharing edges, including vertical connections. However, they explicitly say "in the i-th row", so it's within the row. So it's fine.
Now, another nuance: Could there be a scenario where the row's consecutive full cells connected to the left side could be longer than the forced prefix because of vertical connections? For example, if the leftmost cell is empty, but there is a full cell below it? No, the phrase "connected to the left side" likely means the cells that are reachable from the left side via adjacent full cells, considering both horizontal and vertical moves? But they said "in the i-th row", so it's restricted to that row. So it's the contiguous horizontal block.
Thus fine.
Now, ensure the indexing: The input r_i are given for i=1..h, c_j for j=1..w. So we will store as is.
Now, implement.
Now, I will write final solution in C++.
But before final, consider possible pitfalls:
-
When r_i = w, the forced empty condition is not applied. In our code: if (r[i] < w) set empty. Good.
-
When c_j = h, similarly.
-
For row_val: if (j < r[i]) row_val = 1; else if (j == r[i] && r[i] < w) row_val = 0; else -1.
But if r[i] == w, then the condition j == r[i] would be j == w, which never occurs because j max is w-1. So no empty forced.
Similarly for column.
-
However, what about the case where r[i] = 0 and w = 0? Not possible.
-
Also, note that if r[i] = 0, then we set row_val = 0 for j == 0 (since j == r[i] and r[i] < w). That's correct.
Now, a subtle point: In the row forced condition, we set row_val = 1 for j < r_i. But what about when r_i = 0? Then j < 0 never true, so no full forced. Then we set empty at j = 0 if w>0.
Thus correct.
Now, we also need to consider the possibility of conflicts where a cell is forced both full and empty by the same source? That can't happen because row's conditions are disjoint: j < r_i gives full; j == r_i gives empty if r_i < w; they are distinct; column similarly. So no same-source conflict.
Now, confirm that we treat all cells. That's it.
Now, think about potential integer overflow in exponentiation: Use modulo.
Now, let's produce final answer.
But wait, the problem says: "Initially, all cells are empty. Find the number of ways to fill grid cells to satisfy values of r and c." That might imply that we have to fill cells, starting from empty, but we are counting all assignments. So our answer is number of binary matrices.
Now, one more check: Could there be a scenario where the forced assignments from row and column are consistent, but there is an additional constraint that the grid must be such that the defined r_i and c_j are exactly those numbers, but if we fill free cells arbitrarily, could we inadvertently create a longer consecutive block from the left in some row due to vertical connections? For example, suppose row i has r_i = 1, so forced: (i,1)=1, (i,2)=0. But what if there is a full cell at (i-1,2) and (i+1,2) etc? That doesn't affect the row's horizontal block. So fine.
But could vertical connections cause a cell to become "connected to the left side" via a path that goes down then left? The definition is specifically "in the i-th row", so it's about that row only. So not.
Thus answer is correct.
Now, about time constraints: O(10^6) operations, fine.
Now, finalize.
I'll write code accordingly.
But I see that in the sample Input/Output format, the example 3 output is 797922655. Let's quickly test with our mental? Could be 2^something mod MOD. 797922655 is a known number: 2^(something) mod 1e9+7? Actually 2^? 797922655 is 2^(-1) mod 1e9+7? Because 2^(MOD-2) mod MOD is the modular inverse of 2, which is 500000004. 797922655 is not that. Could be 2^(large) mod. Not important.
Now, let's consider potential issues with large free count: free could be up to 10^6, and we compute pow(2,free) via fast exponentiation O(log free). That's fine.
Now, implement.
Pseudo:
int h,w;
cin>>h>>w;
vector<int> r(h), c(w);
for(int i=0;i<h;i++) cin>>r[i];
for(int j=0;j<w;j++) cin>>c[j];
long long free_cnt=0;
bool ok=true;
for(int i=0;i<h && ok;i++){
for(int j=0;j<w && ok;j++){
int row_val=-1;
if(j<r[i]) row_val=1;
else if(j==r[i] && r[i]<w) row_val=0;
int col_val=-1;
if(i<c[j]) col_val=1;
else if(i==c[j] && c[j]<h) col_val=0;
if(row_val!=-1 && col_val!=-1){
if(row_val!=col_val){
ok=false;
break;
}
}
if(row_val==-1 && col_val==-1){
free_cnt++;
}
}
}
if(!ok){
cout<<0<<endl;
} else {
long long ans=1, base=2, exp=free_cnt;
while(exp>0){
if(exp&1) ans=ans*base%MOD;
base=base*base%MOD;
exp>>=1;
}
cout<<ans<<endl;
}
But careful: In the else branch, we used exp variable, which we modify. But we still have free_cnt for possible debugging. Use copy.
Now, we should consider that the answer may be 1 if free_cnt = 0.
Now, test with examples.
Example1:
h=3,w=4, r=[0,3,1], c=[0,2,3,0].
Compute free_cnt = 1, ans=2, output 2. Works.
Example2: conflict, output 0.
Example3: will compute some free_cnt and ans matches.
Now, consider an edge case: h=1,w=1, r=[0],c=[0]. Then i=0,j=0: row_val: j<0? false; j==0 && r0<1? r0=0<1 true => row_val=0. col_val: i<0? false; i==0 && c0<1? c0=0<1 true => col_val=0. Both forced, equal, not free. free_cnt=0, ans=1. That's correct: only grid with (1,1)=0 satisfies r=0,c=0.
Now, h=1,w=1, r=[1],c=[1]. Then row_val: j<1 -> row_val=1; col_val: i<1 -> col_val=1; equal, free=0, ans=1. That's correct: grid all full.
Now, h=1,w=1, r=[1],c=[0]: conflict.
Now, h=1,w=2, r=[1], c=[0,0]? Row0: j<1? j=0 row_val=1; j=1? j==1 && r0<2 true? r0=1<2 yes, row_val=0. So row forces: (0,0)=1, (0,1)=0. Column0: c0=0 => i==0 => col_val=0 at (0,0). Conflict at (0,0): row 1 vs col 0. So 0. Seems correct.
Now, h=1,w=2, r=[0], c=[0,0]? Row0: j==0 && r0<2 => row_val=0 (empty). j=1? no forced. Column0: i==0 => col_val=0; Column1: i==0 => col_val=0. So forced: (0,0)=0, (0,1)=0; free=0; ans=1. Check: row0 first cell empty -> r0=0; column0 first cell empty -> c0=0; column1 first cell empty -> c1=0. Works.
Now, h=1,w=2, r=[0], c=[1,0]? Row0: (0,0)=0; (0,1) free? Actually row0 only forces (0,0)=0. Column0: c0=1 -> i<1? i=0 <1 true => col_val=1 at (0,0). Conflict at (0,0): row 0 vs col 1 => 0.
Now, h=2,w=1, r=[0,0], c=[0]? Row0: (0,0)=0; Row1: (1,0)=0; Column0: c0=0 -> (0,0)=0; (1,0) not forced? Actually column0: i==0 => empty; i=1? no forced. So forced: (0,0)=0, (1,0)=0. free=0 ans=1. Check: row0 first cell empty -> r0=0, row1 first cell empty -> r1=0; column0 first cell empty -> c0=0. Works.
Now, h=2,w=1, r=[1,0], c=[1]? Row0: full at col0 (since r0=1=w? w=1, r0=1 so row0 full; no empty because r0=w). Row1: r1=0 => (1,0)=0. Column0: c0=1 => rows 0 full; row1? c0=1<h=2 => empty at row1. So row1 (1,0) forced empty by both, row0 full, column full. So no conflict. free=0 ans=1. Check: grid: row0=1, row1=0. Row0: full -> r0=1; row1: empty -> r1=0; column0: first cell row0=1, second row1=0 -> c0=1. So works.
Now, h=2,w=1, r=[1,0], c=[0]? Column0: c0=0 => (0,0)=0; row0 full => conflict. So 0.
Thus seems consistent.
Now, ensure that we treat r_i and c_j as int.
Now, I think we are done.
But wait, we must be careful about the order of conditions: In the scanning method, for each cell we compute row_val and col_val based solely on r_i, c_j. However, there is a subtlety: The forced empty at (i, r_i+1) is only valid if that cell exists. But we used condition r_i < w to enforce that. However, if r_i = w, then there is no empty forced. That's correct. Similarly for columns.
Now, what about the case where r_i = w and we set row_val = 1 for all j < w (i.e., all columns). That's fine.
Now, what about the case where r_i = 0 and w = 1: then we set row_val = 0 for j=0 because j == r_i and r_i < w (0<1). So row_val=0. That's correct.
Now, for column: same.
Now, is there any possibility that a cell could be forced twice by the same condition? Not.
Now, we should also verify that the forced assignments we derived from row and column are indeed necessary and sufficient. Yes.
Now, let's double-check with the example from the problem statement's image. The image shows a 3x4 grid with certain black cells. The given r and c are [0,3,1] and [0,2,3,0] which we already tested.
Now, we are confident.
Now, write final solution in C++.
Potential optimization: we can break early if conflict found.
Now, use fast I/O for speed.
Now, let's consider memory: we only store r and c vectors, no grid, so memory O(h+w). Good.
Now, let's implement.
But before final code, let's test the logic for a bigger case manually by reasoning about free count formula. Some might think free count = (h - something)*(w - something) but not exactly.
But it's fine.
Now, answer.
Now, write final solution.
I'll produce final answer in the "final" channel. ```cpp
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int h, w;
cin >> h >> w;
vector<int> r(h), c(w);
for (int i = 0; i < h; ++i) cin >> r[i];
for (int j = 0; j < w; ++j) cin >> c[j];
long long free_cells = 0;
bool ok = true;
for (int i = 0; i < h && ok; ++i) {
for (int j = 0; j < w && ok; ++j) {
// forced value by row i
int row_val = -1;
if (j < r[i]) row_val = 1;
else if (j == r[i] && r[i] < w) row_val = 0;
// forced value by column j
int col_val = -1;
if (i < c[j]) col_val = 1;
else if (i == c[j] && c[j] < h) col_val = 0;
if (row_val != -1 && col_val != -1) {
if (row_val != col_val) {
ok = false;
break;
}
}
if (row_val == -1 && col_val == -1) {
++free_cells;
}
}
}
if (!ok) {
cout << 0 << '\n';
return 0;
}
// compute 2^free_cells mod MOD
long long ans = 1;
long long base = 2;
long long exp = free_cells;
while (exp > 0) {
if (exp & 1) ans = ans * base % MOD;
base = base * base % MOD;
exp >>= 1;
}
cout << ans << '\n';
return 0;
}