Suppose there is a $h \times w$ grid consisting of empty or full cells. 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 \le i \le 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 \le j \le w$). In particular, $c_j=0$ if the topmost cell of the $j$-th column is empty. 

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.

 [Image] These are the $r$ and $c$ values of some $3 \times 4$ grid. Black cells are full and white cells are empty. 

You have values of $r$ and $c$. Initially, all cells are empty. Find the number of ways to fill grid cells to satisfy values of $r$ and $c$. Since the answer can be very large, find the answer modulo $1000000007\,(10^{9} + 7)$. In other words, find the remainder after division of the answer by $1000000007\,(10^{9} + 7)$.


-----Input-----

The first line contains two integers $h$ and $w$ ($1 \le h, w \le 10^{3}$) — the height and width of the grid.

The second line contains $h$ integers $r_{1}, r_{2}, \ldots, r_{h}$ ($0 \le r_{i} \le w$) — the values of $r$.

The third line contains $w$ integers $c_{1}, c_{2}, \ldots, c_{w}$ ($0 \le c_{j} \le h$) — the values of $c$.


-----Output-----

Print the answer modulo $1000000007\,(10^{9} + 7)$.


-----Examples-----
Input
3 4
0 3 1
0 2 3 0

Output
2

Input
1 1
0
1

Output
0

Input
19 16
16 16 16 16 15 15 0 5 0 4 9 9 1 4 4 0 8 16 12
6 12 19 15 8 6 19 19 14 6 9 16 10 11 15 4

Output
797922655



-----Note-----

In the first example, this is the other possible case.

 [Image] 

In the second example, it's impossible to make a grid to satisfy such $r$, $c$ values.

In the third example, make sure to print answer modulo $(10^9 + 7)$.

Difficulty: interview

Starter code:

Solutions:
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy,time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

H,W = inpl()
rr = inpl()
cc = inpl()

MAP = [[-1]*W for _ in range(H)]

for x,c in enumerate(cc):
    for y in range(c):
        MAP[y][x] = 1
    if c < H:
        MAP[c][x] = 0


for y,r in enumerate(rr):
    for x in range(r):
        if MAP[y][x] == 0:
            print(0)
            return
        MAP[y][x] = 1
    if r < W:
        if MAP[y][r] == 1:
            print(0)
            return
        MAP[y][r] = 0

cnt = 0
for y in range(H):
    for x in range(W):
        if MAP[y][x] == -1:
            cnt += 1

print(pow(2,cnt,mod))


h, w = list(map(int, input().split()))
r = list(map(int, input().split()))
c = list(map(int, input().split()))

a = [[0] * w for i in range(h)]
for i in range(h):
    if r[i] < w:
        a[i][:r[i] + 1] = [1] * r[i] + [-1]
    else:
        a[i] = [1] * w
for j in range(w):
    for i in range(c[j]):
        if a[i][j] == -1:
            print(0)
            break
        a[i][j] = 1
    else:
        if c[j] < h:
            if a[c[j]][j] == 1:
                print(0)
                break              
            a[c[j]][j] = -1
        continue
    break
else:
    cnt0 = sum([row.count(0) for row in a])
    print(2 ** cnt0 % 1000000007)

import sys
from collections import defaultdict
from itertools import combinations
from itertools import permutations
input = lambda : sys.stdin.readline().rstrip()
def write(*args, sep="\n"):
  for i in args:
    sys.stdout.write("{}".format(i) + sep)
INF = float('inf')
MOD = int(1e9 + 7)

r, c = list(map(int, input().split()))

row = list(map(int, input().split()))
column = list(map(int, input().split()))
arr = [ [-1] * (c + 1) for i in range(r + 1) ]

for i in range(r):
  for j in range(row[i]):
      arr[i][j] = 1
    
  arr[i][row[i]] = 0

  #print(arr, "!")

for j in range(c):
  #print(arr)
  for i in range(column[j]):
    if arr[i][j] != 0:
      arr[i][j] = 1
    else:
      print(0)
      return
  
  if arr[column[j]][j] == 1:
    print(0)
    return
  
  arr[column[j]][j] = 0

cnt = 0
for i in range(r):
  for j in range(c):
    cnt += (arr[i][j] == -1)

#print("???", cnt)
print(pow(2, cnt, MOD))


import sys
input = sys.stdin.readline

h,w=list(map(int,input().split()))
R=list(map(int,input().split()))
C=list(map(int,input().split()))

mod=10**9+7

ANS=[[0]*w for i in range(h)]

for i in range(h):
    for j in range(R[i]):
        ANS[i][j]=1

    if R[i]!=w:
        ANS[i][R[i]]=2

#print(ANS)

for i in range(w):
    for j in range(C[i]):
        if ANS[j][i]==2:
            print(0)
            return
        ANS[j][i]=1

    if C[i]!=h:
        if ANS[C[i]][i]==1:
            print(0)
            return
            
        ANS[C[i]][i]=2

count=0
for i in range(h):
    for j in range(w):
        if ANS[i][j]==0:
            count+=1

print(pow(2,count,mod))


h, w = list(map(int,input().split()))

r = list(map(int,input().split()))
c = list(map(int,input().split()))

l = [-1] * (h * w)

for i in range(h):
    tiles = r[i]

    for j in range(tiles):
        l[w * i + j] = 1

    if tiles < w:
        l[w * i + tiles] = 0

out = 1
for j in range(w):
    tiles = c[j]

    for i in range(tiles):
        pos = w * i + j
        if l[pos] == 0:
            out = -1
            break
        l[pos] = 1

    if tiles < h:
        pos = w * (tiles) + j
        if l[pos] == 1:
            out = -1
            break
        l[pos] = 0

M = 10**9+7

if out == -1:
    print(0)
else:
    count = 0
    for i in l:
        if i == -1:
            count += 1
    print(pow(2,count,M))


H, W = list(map(int, input().split()))
R = [int(a) for a in input().split()]
C = [int(a) for a in input().split()]

ans = 0
for i in range(H):
    for j in range(W):
        if (R[i] == j and C[j] > i) or (R[i] > j and C[j] == i):
            print(0)
            return
        if R[i] < j and C[j] < i:
            ans += 1
print(pow(2, ans, 10**9+7))





h,w=map(int,input().split())
r=list(map(int,input().split()))
c=list(map(int,input().split()))
mat=[None]*h
ans=1
for i in range(h):
    mat[i]=[-1]*w
for i in range(h):
    for j in range(r[i]):
        mat[i][j]=0
    if r[i]<w:
        mat[i][r[i]]=1
for j in range(w):
    for i in range(c[j]):
        if(mat[i][j]==1):
            ans=0
        mat[i][j]=0
    if c[j]<h:
        if(mat[c[j]][j]==0):
            ans=0
        mat[c[j]][j]=1
for i in mat:
    for j in i:
        if j==-1:
            ans=(2*ans)%1000000007
print(ans)

h, w = list(map(int, input().split()))

r = list(map(int, input().split()))
c = list(map(int, input().split()))

unset = h * w

field = [[-1] * w for _ in range(h)]

for i in range(h):
    x = r[i]
    row = field[i]
    for j in range(x):
        if row[j] == -1:
            unset -= 1
        row[j] = 1
    if x < w:
        if row[x] == -1:
            unset -= 1
        row[x] = 0

for i in range(w):
    x = c[i]
    for j in range(x):
        if field[j][i] == -1:
            unset -= 1
        elif field[j][i] == 0:
            print(0)
            return
        field[j][i] = 1
    if x < h:
        if field[x][i] == -1:
            unset -= 1
        elif field[x][i] == 1:
            print(0)
            return
        field[x][i] = 0

print(pow(2, unset, 10 ** 9 + 7))



h, w = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

grid = [[-1]*w for i in range(h)]
for i in range(h):
    if a[i] == 0:
        grid[i][0] = 0
        continue
    for j in range(a[i]):
        grid[i][j] = 1
    if 0 <= a[i] < w:
        grid[i][a[i]] = 0

for j in range(w):
    if b[j] == 0:
        if grid[0][j] != 1:
            grid[0][j] = 0
        else:
            print(0)
            return
        continue
    for i in range(b[j]):
        if grid[i][j] != 0:
            grid[i][j] = 1
        else:
            print(0)
            return
    if 0 <= b[j] < h:
        if grid[b[j]][j] != 1:
            grid[b[j]][j] = 0
        else:
            print(0)
            return

MOD = 10**9 + 7
ans = 1
for i in range(h):
    for j in range(w):
        if grid[i][j] == -1:
            ans *= 2
            ans %= MOD
print(ans)

h, w = list(map(int, input().split()))
grid = [[-1 for i in range(w)] for j in range(h)]

isValid = True

rs = list(map(int, input().split()))
for y in range(h):
    for j in range(rs[y]):
        grid[y][j] = 1
    if rs[y] != w:
        grid[y][rs[y]] = 0

cs = list(map(int, input().split()))
for x in range(w):
    for j in range(cs[x]):
        if grid[j][x] == 0:
            isValid = False
        grid[j][x] = 1
    if cs[x] != h:
        if grid[cs[x]][x] == 1:
            isValid = False
        grid[cs[x]][x] = 0

total = 1
for i in range(h):
    for j in range(w):
        if grid[i][j] == -1:
            total *= 2
            total %= 1000000007

if isValid:
    print(total)
else:
    print(0)


MOD = 1000000007

h, w = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
m = [[None]*w for _ in range(h)]
for i in range(h):
	for j in range(r[i]):
		m[i][j] = True
	if r[i] < w: m[i][r[i]] = False
for j in range(w):
	for i in range(c[j]):
		if m[i][j] == False:
			print(0)
			return
		m[i][j] = True
	if c[j] < h:
		if m[c[j]][j] == True:
			print(0)
			return
		m[c[j]][j] = False
res = 1
for i in range(h):
	for j in range(w):
		if m[i][j] == None:
			res = (res*2) % MOD
print(res)

modus = 1000000007

R, C = map(int, input().split())

r_lst = list(map(int, input().split()))
c_lst = list(map(int, input().split()))

M = [[-1 for j in range(C)] for i in range(R)]

impossible = False
for i, r in enumerate(r_lst):

	for index in range(r):
		M[i][index] = 1

	if r != C:
		M[i][r] = 0

for i, c in enumerate(c_lst):

	for index in range(c):
		if M[index][i] == 0:
			impossible = True
			break

		M[index][i] = 1

	if impossible:
		break

	if c != R:

		if M[c][i] == 1:
			impossible = True
			break

		M[c][i] = 0

if impossible:
	print(0)
else:
	count = 0
	for i in range(R):
		for j in range(C):
			if M[i][j] == -1:
				count += 1

	result = pow(2, count, modus)
	print(result)

h, w = list(map(int, input().split()))
r = list(map(int, input().split()))
c = list(map(int, input().split()))

table = [[-1]*w for i in range(h)]

for i, row in enumerate(r):
    for j in range(row):
        table[i][j] = 1
    if row < w:
        table[i][row] = 0


for j, col in enumerate(c):
    for i in range(col):
        if table[i][j] == 0:
            print(0)
            return
        table[i][j] = 1
    if col < h:
        if table[col][j] == 1:
            print(0)
            return
        table[col][j] = 0

res = 1

for i in range(h):
    for j in range(w):
        if table[i][j] == -1:
            res = (res*2)%1000000007

print(res)



rows, cols = list(map(int, input().split()))

a = [[-1] * cols for _ in range(rows)]

r = list(map(int, input().split()))
c = list(map(int, input().split()))

# print(np.matrix(a))


for row in range(rows):
  for col in range(r[row]):
    a[row][col] = 1
  
  if(r[row] < cols):
    a[row][r[row]] = 0 

# print(np.matrix(a))


for col in range(cols):
  for row in range(c[col]):
    a[row][col] = 1
  
  if(c[col] < rows):
    a[c[col]][col] = 0 

# print(np.matrix(a))

isSol = True

for row in range(rows):
  for col in range(r[row]):
    if a[row][col] != 1 :
      isSol = False

  
  if(r[row] < cols):
    if a[row][r[row]] != 0:
      isSol = False

for col in range(cols):
  for row in range(c[col]):
    if(a[row][col] != 1):
      isSol = False
  
  if(c[col] < rows):
    if (a[c[col]][col] != 0):
      isSol = False

if(isSol):
  sol = 1

  for row in a:
    for i in row:
      if(i == -1):
        sol = (sol * 2) % 1000000007
  
  print(sol)
else:
  print(0)




h,w=[int(x) for x in input().split()]
a=[int(x) for x in input().split()]
b=[int(x) for x in input().split()]
tab=[[0]*w for i in range(h)]
arr=0
for i in range(h):
    if a[i]==0:
        tab[i][0]=2
    else:
        for j in range(a[i]):
            tab[i][j]=1
        if a[i]<w:
            tab[i][a[i]]=2
for i in range(w):
    if b[i]==0:
        if tab[0][i]==1:
            print(0)
            return
        tab[0][i]=2
    else:
        for j in range(b[i]):
            if tab[j][i]==2:
                print(0)
                return
            tab[j][i]=1
        if b[i]<h:
            if tab[b[i]][i]==1:
                print(0)
                return
            tab[b[i]][i]=2
for i in range(h):
    for j in range(w):
        if tab[i][j]==0:
            arr+=1
print(pow(2,arr,(10**9+7)))
        
               


import sys
input = sys.stdin.readline

def main():
    H, W = map(int, input().split())
    R = list(map(int, input().split()))
    C = list(map(int, input().split()))

    grid1 = [[-1]*W for _ in range(H)]
    grid2 = [[-1]*W for _ in range(H)]

    for i, r in enumerate(R):
        for j in range(r):
            grid1[i][j] = 1
        if r <= W-1:
            grid1[i][r] = 0

    for i, c in enumerate(C):
        for j in range(c):
            grid2[j][i] = 1
        if c <= H-1:
            grid2[c][i] = 0

    ok = True
    prob = 0
    for h in range(H):
        for w in range(W):
            if grid1[h][w] == -1 and grid2[h][w] == -1:
                prob += 1
            elif grid1[h][w] == -1 or grid2[h][w] == -1:
                continue
            elif grid1[h][w] != grid2[h][w]:
                ok = False

    if not ok:
        print(0)
    else:
        mod = int(1E9+7)
        ans = 1
        for _ in range(prob):
            ans = ans * 2 % mod
        print(ans)

def __starting_point():
    main()
__starting_point()

'''
Created on 2019. 9. 21.

@author: kkhh88
'''
'''
a, b = map(int,input().split(' '))

t = 0

for i in range(a, b+1):
    x = [0] * 10
    n = i
    while n:
        x[n%10] = x[n%10] + 1
        n = n // 10
    n = 0
    for n in range(10):
        if x[n] > 1:
            break
    
    if n == 9 and x[n] < 2:
        print (i)
        t = 1
        break
    
if t == 0:
    print (-1)
'''

h, w = list(map(int,input().split(' ')))
r = list(map(int,input().split(' ')))
c = list(map(int,input().split(' ')))

cnt = 0
x = [[-1 for _ in range(w)] for _ in range(h)]
for i in range(w):
    for j in range(c[i]):
        if x[j][i] == 1 or x[j][i] == -1:
            x[j][i] = 1
        else:
            cnt = -1
            break
    if cnt < 0:
        break
    if c[i] < h:
        if x[c[i]][i] == 0 or x[c[i]][i] == -1:
            x[c[i]][i] = 0
        else:
            cnt = -1
            break

for i in range(h):
    for j in range(r[i]):
        if x[i][j] == 1 or x[i][j] == -1:
            x[i][j] = 1
        else:
            cnt = -1
            break
    if cnt < 0:
        break
    if r[i] < w:
        if x[i][r[i]] == 0 or x[i][r[i]] == -1:
            x[i][r[i]] = 0 
        else:
            cnt = -1
            break

if cnt < 0:
    print(0)
else:
    for i in range(w):
        for j in range(h):
            if x[j][i] == -1:
                cnt = cnt + 1
    print((1<<cnt) % 1000000007)


h, w = list(map(int, input().split()))
rows = list(map(int, input().split()))
cols = list(map(int, input().split()))
matrix = [None] * h
answer = 0
found = False
for i in range(h):
    matrix[i] = [-1] * w
for i in range(h):
    r = rows[i]
    if r == 0:
        matrix[i][0] = 0
    else:
        for j in range(r):
            matrix[i][j] = 1
        if r < w:
            matrix[i][r] = 0
for i in range(w):
    c = cols[i]
    if c == 0:
        if matrix[0][i] == 1:
            found = True
        matrix[0][i] = 0
    else:
        for j in range(c):
            if matrix[j][i] == 0:
                found = True
                break
            matrix[j][i] = 1
        if c < h:
            if matrix[c][i] == 1:
                found = True
            matrix[c][i] = 0
if not found:
    cnt = 0
    for i in range(h):
        for j in range(w):
            if matrix[i][j] < 0:
                cnt += 1
    answer = (2 ** cnt) % 1000000007
print(answer)


''' CODED WITH LOVE BY SATYAM KUMAR '''

from sys import stdin, stdout
import heapq
import cProfile, math
from collections import Counter, defaultdict, deque
from bisect import bisect_left, bisect, bisect_right
import itertools
from copy import deepcopy
from fractions import Fraction
import sys, threading
import operator as op
from functools import reduce
import sys

sys.setrecursionlimit(10 ** 6)  # max depth of recursion
threading.stack_size(2 ** 27)  # new thread will get stack of such size
fac_warm_up = False
printHeap = str()
memory_constrained = False
P = 10 ** 9 + 7


class MergeFind:
    def __init__(self, n):
        self.parent = list(range(n))
        self.size = [1] * n
        self.num_sets = n
        self.lista = [[_] for _ in range(n)]

    def find(self, a):
        to_update = []
        while a != self.parent[a]:
            to_update.append(a)
            a = self.parent[a]
        for b in to_update:
            self.parent[b] = a
        return self.parent[a]

    def merge(self, a, b):
        a = self.find(a)
        b = self.find(b)
        if a == b:
            return
        if self.size[a] < self.size[b]:
            a, b = b, a
        self.num_sets -= 1
        self.parent[b] = a
        self.size[a] += self.size[b]
        self.lista[a] += self.lista[b]

    def set_size(self, a):
        return self.size[self.find(a)]

    def __len__(self):
        return self.num_sets


def display(string_to_print):
    stdout.write(str(string_to_print) + "\n")


def prime_factors(n):  # n**0.5 complex
    factors = dict()
    for i in range(2, math.ceil(math.sqrt(n)) + 1):
        while n % i == 0:
            if i in factors:
                factors[i] += 1
            else:
                factors[i] = 1
            n = n // i
    if n > 2:
        factors[n] = 1
    return (factors)


def all_factors(n):
    return set(reduce(list.__add__,
                      ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))


def fibonacci_modP(n, MOD):
    if n < 2: return 1
    return (cached_fn(fibonacci_modP, (n + 1) // 2, MOD) * cached_fn(fibonacci_modP, n // 2, MOD) + cached_fn(
        fibonacci_modP, (n - 1) // 2, MOD) * cached_fn(fibonacci_modP, (n - 2) // 2, MOD)) % MOD


def factorial_modP_Wilson(n, p):
    if (p <= n):
        return 0
    res = (p - 1)
    for i in range(n + 1, p):
        res = (res * cached_fn(InverseEuler, i, p)) % p
    return res


def binary(n, digits=20):
    b = bin(n)[2:]
    b = '0' * (digits - len(b)) + b
    return b


def is_prime(n):
    """Returns True if n is prime."""
    if n < 4:
        return True
    if n % 2 == 0:
        return False
    if n % 3 == 0:
        return False
    i = 5
    w = 2
    while i * i <= n:
        if n % i == 0:
            return False
        i += w
        w = 6 - w
    return True


def generate_primes(n):
    prime = [True for i in range(n + 1)]
    p = 2
    while p * p <= n:
        if prime[p]:
            for i in range(p * 2, n + 1, p):
                prime[i] = False
        p += 1
    return prime


factorial_modP = []


def warm_up_fac(MOD):
    nonlocal factorial_modP, fac_warm_up
    if fac_warm_up: return
    factorial_modP = [1 for _ in range(fac_warm_up_size + 1)]
    for i in range(2, fac_warm_up_size):
        factorial_modP[i] = (factorial_modP[i - 1] * i) % MOD
    fac_warm_up = True


def InverseEuler(n, MOD):
    return pow(n, MOD - 2, MOD)


def nCr(n, r, MOD):
    nonlocal fac_warm_up, factorial_modP
    if not fac_warm_up:
        warm_up_fac(MOD)
        fac_warm_up = True
    return (factorial_modP[n] * (
            (pow(factorial_modP[r], MOD - 2, MOD) * pow(factorial_modP[n - r], MOD - 2, MOD)) % MOD)) % MOD


def get_int():
    return int(stdin.readline().strip())


def get_tuple():
    return list(map(int, stdin.readline().split()))


def get_list():
    return list(map(int, stdin.readline().split()))


memory = dict()


def clear_cache():
    nonlocal memory
    memory = dict()


def cached_fn(fn, *args):
    nonlocal memory
    if args in memory:
        return memory[args]
    else:
        result = fn(*args)
        memory[args] = result
        return result


def ncr(n, r):
    return math.factorial(n) / (math.factorial(n - r) * math.factorial(r))


def binary_search(i, li):
    fn = lambda x: li[x] - x // i
    x = -1
    b = len(li)
    while b >= 1:
        while b + x < len(li) and fn(b + x) > 0:  # Change this condition 2 to whatever you like
            x += b
        b = b // 2
    return x


# -------------------------------------------------------------- MAIN PROGRAM


TestCases = False
optimise_for_recursion = True  # Can not be used clubbed with TestCases WHen using recursive functions, use Python 3


def main():
    h, w = get_tuple()
    hi = get_list()
    wi = get_list()
    mat = [[0 for _ in range(w)] for _ in range(h)]
    i, j = 0,0
    for i, ele in enumerate(hi):
        for j in range(0, ele):
            mat[i][j] = 1
        if ele<w: mat[i][ele]=-1
    #[print(li) for li in mat]
    for i, ele in enumerate(wi):
        for j in range(0, ele):
            if mat[j][i]==-1:
                print(0)
                return
            mat[j][i] = 1
        if ele<h:
            if mat[ele][i]==1:
                #print(ele, i)
                print(0)
                return
            mat[ele][i]=-1
    #[print (li) for li in mat]
    res = 1
    for li in mat:
        for i in li:
            if i==0:
                res = (res*2)%P
    print(res)




# --------------------------------------------------------------------- END=


if TestCases:
    for i in range(get_int()):
        main()
else:
    main() if not optimise_for_recursion else threading.Thread(target=main).start()


rt, ct = map(int, input().split())
grid = [[-1 for x in range(ct)] for y in range(rt)]

rows = list(map(int, input().split()))
cols = list(map(int, input().split()))

can = True
for r in range(len(rows)):
	rc = rows[r];
	for i in range(rc):
		if(grid[r][i] == 0):
			can = False
			break
		grid[r][i] = 1
	if (rc < ct):
		if (grid[r][rc] == 1):
			can = False
			break
		grid[r][rc] = 0
	
for c in range(len(cols)):
	if(not can):
		break
	cc = cols[c];
	for i in range(cc):
		if(grid[i][c] == 0):
			can = False
			break
		grid[i][c] = 1
	if (cc < rt):
		if(grid[cc][c] == 1):
			can = False
			break
		grid[cc][c] = 0

if(not can):
	print(0)
else:
	cnt = 0
	for i in range(len(grid)):
		row = grid[i]
		for j in row:
			if (j == -1):
				cnt += 1
	print(pow(2,cnt, (10**9+7)))

h, w = tuple(map(int, input().split()))

arr = [['r'] * w for i in range(h)]

H = list(map(int, input().split()))
for i, j in enumerate(H):
    arr[i][:j] = ['b'] * j
    if j < w:
        arr[i][j] = 'w'

W = list(map(int, input().split()))
error = False
for i, j in enumerate(W):
    for k in range(j):
        if arr[k][i] == 'w':
            error = True
            break
        else:
            arr[k][i] = 'b'
    if j < h and arr[j][i] == 'b':
        error = True
        break
    elif j < h:
        arr[j][i] = 'w'

#for i in arr:
#    print(*i)

if error:
    print(0)
else:
    count = 0
    for i in arr:
        count += i.count('r')
    print((2 ** count) % (10 ** 9 + 7))


mod = 1000000007
res = 1
h, w = list(map(lambda x:int(x),input().split()))
pole = [[0 for x in range(w)] for y in range(h)]
rs = list(map(lambda x:int(x),input().split()))
ls = list(map(lambda x:int(x),input().split()))
for i in range(h):
    for x in range(rs[i]):
        pole[i][x] = 1
    if rs[i] != w:
        pole[i][rs[i]] = -1
#print(pole)
for i in range(w):
    for y in range(ls[i]):
        if pole[y][i] == -1:
            print(0)
            return
        else:
            pole[y][i] = 1
    if ls[i] != h:
        if pole[ls[i]][i] == 1:
            print(0)
            return
        pole[ls[i]][i] = -1
for y in pole:
    for x in y:
        if x == 0:
            res<<=1
            res = res % mod
print(res)

h,w = list(map(int,input().split()))
'''arr = [[0] * w for i in range(h)]
arr[0] = [1] * w
for i in range(1,h):
    arr[i][0] = 1'''
mod = 1000000007
arrr = list(map(int,input().split()))
arrc = list(map(int,input().split()))

ans = 1
for i in range(h):
    for j in range(w):
        if j > arrr[i]:
            break
        elif j == arrr[i]:
            if arrc[j] > i:
                ans = 0
                break
        elif arrc[j] == i:
            ans = 0
            break
    if ans == 0:
        break
else:
    for j in range(w):
        for i in range(h):
            if i > arrc[j]:
                break
            elif i == arrc[j]:
                if arrr[i] > j:
                    ans = 0
                    break
            elif arrr[i] == j:
                ans = 0
                break
        if ans == 0:
            break
if ans == 1:
    for i in range(1,h):
        for j in range(1,w):
            if j > arrr[i] and i > arrc[j]:
                #print(i,j)
                ans *= 2
                if ans > mod:
                    ans -= mod
print(ans)




def main():
    from array import array
    import sys
    h, w = map(int, sys.stdin.readline().split())
    grid = tuple((tuple((array('b', (0,)) for _ in range(w))) for _ in range(h)))
    ans = h * w
    arr1 = array('b', (1,))
    for _ in range(2):
        for row, r in zip(grid, map(int, sys.stdin.readline().split())):
            arr1r = arr1 * r
            arr1r.append(-1)
            for elem, value in zip(row, array('b', arr1r)):
                if not elem[0]:
                    elem[0] = value
                    ans -= 1
                elif elem[0] != value:
                    return print('0')
        grid = zip(*grid)
    return print(pow(2, ans, 1000000007))


main()