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: MEDIUM
Tags: Mathematics

Implementation

Starter code:

Solutions:
(h, w) = map(int, input().split())
r = [*map(int, input().split())]
c = [*map(int, input().split())]
m = []
for i in range(h):
	m.append([1] * r[i] + [0] * (w - r[i]))
for i in range(w):
	for j in range(c[i]):
		m[j][i] = 1
ans = 0
for i in range(h):
	t = 0
	for j in range(w):
		if m[i][j] == 1:
			t += 1
		else:
			break
	if r[i] != t:
		ans = -1
for i in range(w):
	t = 0
	for j in range(h):
		if m[j][i] == 1:
			t += 1
		else:
			break
	if c[i] != t:
		ans = -1
if ans == -1:
	print(0)
	exit()
for i in range(h):
	t = True
	for j in range(w):
		if t and m[i][j] == 0:
			t = False
		m[i][j] = [m[i][j], t]
for i in range(w):
	t = True
	for j in range(h):
		if t and m[j][i][0] == 0:
			t = False
		m[j][i].append(t)
for i in range(1, h):
	for j in range(1, w):
		if m[i - 1][j][2] == m[i][j - 1][1] == False:
			ans += 1
print(pow(2, ans, int(1000000000.0 + 7)))


def make_grid(r, c, row, col):
	mod = 10 ** 9 + 7
	grid = [[-1 for i in range(c)] for _ in range(r)]
	for i in range(r):
		k = row[i]
		for j in range(k):
			grid[i][j] = 'b'
		if k < c:
			grid[i][k] = 's'
	for i in range(c):
		k = col[i]
		for j in range(k):
			if grid[j][i] == 's':
				return 0
			grid[j][i] = 'b'
		if k < r:
			if grid[k][i] == 'b':
				return 0
			grid[k][i] = 's'
	count = 0
	ans = 1
	for i in range(r):
		for j in range(c):
			if grid[i][j] == -1:
				count += 1
				ans = ans * 2
				ans = ans % mod
	return ans % mod
(r, c) = map(int, input().split())
row = list(map(int, input().split()))
col = list(map(int, input().split()))
print(make_grid(r, c, row, col))


from sys import stdin
input = stdin.readline
m = 10 ** 9 + 7
R = lambda : map(int, input().split())
(r, c) = R()
p = 0
a = list(R())
b = list(R())
for i in range(b[0]):
	if a[i] == 0:
		exit(print(0))
for i in range(a[0]):
	if b[i] == 0:
		exit(print(0))
for i in range(1, r + 1):
	for j in range(1, c + 1):
		if j == a[i - 1] + 1 and i <= b[j - 1]:
			exit(print(0))
		elif i == b[j - 1] + 1 and j <= a[i - 1]:
			exit(print(0))
		if j > a[i - 1] + 1 and i > b[j - 1] + 1:
			p += 1
print(2 ** p % m)


import sys
input = sys.stdin.readline
(h, w) = 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
for i in range(w):
	for j in range(C[i]):
		if ANS[j][i] == 2:
			print(0)
			sys.exit()
		ANS[j][i] = 1
	if C[i] != h:
		if ANS[C[i]][i] == 1:
			print(0)
			sys.exit()
		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))


def f():
	changed = 0
	(h, w) = map(int, input().split())
	grid = [[0] * w for _ in range(h)]
	rs = list(map(int, input().split()))
	cs = list(map(int, input().split()))
	for r in range(h):
		if rs[r] > w:
			print(0)
			return
		for c in range(rs[r]):
			changed += 1
			grid[r][c] = 1
		if rs[r] < w:
			changed += 1
			grid[r][rs[r]] = 2
	for c in range(w):
		if cs[c] > h:
			print(0)
			return
		for r in range(cs[c]):
			if grid[r][c] == 2:
				print(0)
				return
			if grid[r][c] == 0:
				grid[r][c] = 1
				changed += 1
		if cs[c] < h:
			if grid[cs[c]][c] == 1:
				print(0)
				return
			if grid[cs[c]][c] == 0:
				changed += 1
				grid[cs[c]][c] = 2
	result = 1
	for i in range(h * w - changed):
		result *= 2
		result %= 1000000007
	print(result)
f()


mod = 10 ** 9 + 7
mat = [[0 for i in range(1005)] for i in range(1005)]
(h, w) = map(int, input().split())
flag = True
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(h):
	for j in range(a[i]):
		mat[i][j] = 1
	mat[i][a[i]] = 2
for i in range(w):
	for j in range(b[i]):
		if mat[j][i] == 2:
			flag = False
		mat[j][i] = 1
	if mat[b[i]][i] == 1:
		flag = False
	mat[b[i]][i] = 2
count = 1
for i in range(h):
	for j in range(w):
		if mat[i][j] == 0:
			count = count * 2
			count = count % mod
if flag == False:
	count = 0
print(count)


(h, w) = map(int, input().split())
c = list()
from sys import exit
for _ in range(h):
	c.append([2] * w)
for (i, e) in enumerate(input().split()):
	if int(e) == w:
		c[i] = [1] * w
	else:
		for j in range(int(e)):
			c[i][j] = 1
		c[i][int(e)] = 0
for (i, e) in enumerate(input().split()):
	for j in range(int(e)):
		if not c[j][i]:
			print(0)
			exit()
		c[j][i] = 1
	if int(e) != h:
		if c[int(e)][i] == 1:
			print(0)
			exit()
		c[int(e)][i] = 0
ans = 0
for i in range(h):
	for j in range(w):
		ans += c[i][j] == 2
print(2 ** ans % (10 ** 9 + 7))


def gridfill(r, c):
	empty = 0
	for i in range(len(r)):
		for j in range(len(c)):
			if j > r[i] and i > c[j]:
				empty += 1
			if i < c[j] and j == r[i]:
				return 0
			if i == c[j] and j < r[i]:
				return 0
	return 2 ** empty % 1000000007

def io():
	(h, w) = map(int, input().split())
	r = list(map(int, input().split()))
	c = list(map(int, input().split()))
	print(gridfill(r, c))
io()


(h, w) = [int(z) for z in input().split()]
r = [int(z) for z in input().split()]
c = [int(z) for z in input().split()]
mod = 10 ** 9 + 7
grid = [['?' for col in range(w + 1)] for row in range(h + 1)]

def try_set(row, col, target):
	if grid[row][col] == '?':
		grid[row][col] = target
	elif grid[row][col] != target:
		raise ValueError

def go():
	try:
		for row in range(h):
			for col in range(r[row]):
				try_set(row, col, 'FULL')
			try_set(row, r[row], 'EMPTY')
		for col in range(w):
			for row in range(c[col]):
				try_set(row, col, 'FULL')
			try_set(c[col], col, 'EMPTY')
	except ValueError:
		return 0
	answer = 1
	for row in range(h):
		for col in range(w):
			if grid[row][col] == '?':
				answer = answer * 2 % mod
	return answer
print(go())


(h, w) = list(map(int, input().split()))
wVal = list(map(int, input().split()))
hVal = list(map(int, input().split()))
grid = []
for i in range(h):
	grid.append(['0'] * w)
fl = True
for i in range(h):
	for g in range(wVal[i]):
		grid[i][g] = 'b'
	if wVal[i] != w:
		grid[i][wVal[i]] = 'w'
for i in range(w):
	for g in range(hVal[i]):
		if grid[g][i] == 'w':
			fl = False
		grid[g][i] = 'b'
	if hVal[i] != h:
		if grid[hVal[i]][i] == 'b':
			fl = False
		grid[hVal[i]][i] = 'w'
c = 0
for i in range(h):
	for g in range(w):
		c += grid[i][g] == '0'
if fl:
	print(pow(2, c, 1000000007))
else:
	print(0)


def CountOfZero(d):
	count = 0
	for i in d:
		count += i.count(0)
	return count
(h, w) = [int(i) for i in input().split()]
r = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]
p = [[0] * w for i in range(h)]
isCan = True
for i in range(h):
	for j in range(r[i]):
		p[i][j] = 1
	if r[i] < w:
		p[i][r[i]] = 2
for i in range(w):
	if c[i] != 0:
		for j in range(c[i]):
			if p[j][i] == 0 or p[j][i] == 1:
				p[j][i] = 1
			else:
				isCan = False
				break
	if c[i] < h:
		if p[c[i]][i] != 1:
			p[c[i]][i] = 2
		else:
			isCan = False
			break
count = CountOfZero(p)
print(2 ** count % (int(1000000000.0) + 7) if isCan else 0)


import math
import bisect
from functools import reduce
from collections import defaultdict

def power(x, y, p):
	res = 1
	x = x % p
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def inn():
	return int(input())

def inl():
	return list(map(int, input().split()))
MOD = 10 ** 9 + 7
INF = inf = 10 ** 18 + 5
(h, w) = inl()
r = inl()
c = inl()
tab = [[0] * w for i in range(h)]
f = 1
for i in range(h):
	for j in range(r[i]):
		tab[i][j] = 1
for i in range(w):
	for j in range(c[i]):
		if tab[j][i] == 1:
			tab[j][i] = 3
		else:
			tab[j][i] = 2
for i in range(h):
	j = 0
	while j < w and tab[i][j] != 0:
		j += 1
	if j > r[i]:
		f = 0
		break
for i in range(w):
	j = 0
	while j < h and tab[j][i] != 0:
		j += 1
	if j > c[i]:
		f = 0
		break
if f == 0:
	print(0)
else:
	empc = 0
	for i in range(1, h):
		for j in range(1, w):
			if tab[i][j] == 0 and tab[i][j - 1] not in [1, 3] and (tab[i - 1][j] not in [2, 3]):
				empc += 1
	print(power(2, empc, MOD))


(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)


mod = 10 ** 9 + 7
(n, k) = map(int, input().split())
l1 = list(map(int, input().split()))
l2 = list(map(int, input().split()))
s = 1
for i in range(len(l1)):
	for j in range(len(l2)):
		if j == l1[i]:
			if i + 1 <= l2[j]:
				s = 0
		if i == l2[j]:
			if j + 1 <= l1[i]:
				s = 0
for i in range(len(l1)):
	for j in range(l1[i] + 1, len(l2)):
		if l2[j] == 0 and i == 0:
			continue
		if i + 1 > l2[j] + 1:
			s = s * 2 % mod
print(s % mod)


(h, w) = map(int, input().split())
r = [int(n) for n in input().split()]
c = [int(n) for n in input().split()]
f = True
k = h * w
a = []
for i in range(h):
	b = []
	for j in range(w):
		b.append(2)
	a.append(b)
(i, j) = (0, 0)
while i < h:
	if r[i] == 0:
		if a[i][j] == 2:
			k -= 1
		a[i][j] = 0
	if r[i] > 0:
		for t in range(r[i]):
			if a[i][j] == 2:
				k -= 1
			a[i][j] = 1
			j += 1
		if j < w:
			if a[i][j] == 2:
				k -= 1
			a[i][j] = 0
		j = 0
	i += 1
i = 0
j = 0
while j < w:
	if not f:
		break
	if c[j] == 0:
		if a[i][j] == 2:
			k -= 1
		if a[i][j] == 1:
			f = False
			break
		a[i][j] = 0
	if c[j] > 0:
		for t in range(c[j]):
			if a[i][j] == 2:
				k -= 1
			if a[i][j] == 0:
				f = False
				break
			a[i][j] = 1
			i += 1
		if i < h:
			if a[i][j] == 2:
				k -= 1
			if a[i][j] == 1:
				f = False
				break
			a[i][j] = 0
		i = 0
	j += 1
if f:
	l = 1
	for t in range(k):
		l = l * 2 % 1000000007
	print(l)
else:
	print(0)


import sys
(h, w) = [int(i) for i in input().split()]
r = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]
t = [[0 for j in range(w)] for i in range(h)]
for i in range(h):
	j = -1
	for j in range(r[i]):
		t[i][j] = 1
	if j + 1 != w:
		t[i][j + 1] = -1
for i in range(w):
	j = -1
	for j in range(c[i]):
		if t[j][i] == -1:
			print(0)
			sys.exit()
		else:
			t[j][i] = 1
	if j + 1 != h:
		if t[j + 1][i] == 1:
			print(0)
			sys.exit()
		else:
			t[j + 1][i] = 1
a = 0
for i in range(h):
	for j in range(w):
		if t[i][j] == 0:
			a += 1
print(2 ** a % 1000000007)


(h, w) = map(int, input().split())
h1 = [int(i) for i in input().split()]
w1 = [int(i) for i in input().split()]
count = 0
for i in range(h):
	for j in range(w):
		if w1[j] == i and h1[i] > j or (h1[i] == j and w1[j] > i):
			print(0)
			exit()
		if j > h1[i] and i > w1[j]:
			count += 1
print(pow(2, count, 10 ** 9 + 7))


def fill(M, r, c, h, w, mod):
	for i in range(h):
		limit = range(r[i] + 1)
		for j in limit:
			try:
				if j == limit[-1]:
					M[i][j] = -1
				else:
					M[i][j] = 0
			except IndexError:
				pass
	for j in range(w):
		limit = range(c[j] + 1)
		for i in limit:
			try:
				if i != limit[-1] and M[i][j] == -1:
					return 0
				elif i == limit[-1] and M[i][j] == 0:
					return 0
				else:
					M[i][j] = 0
			except IndexError:
				pass
	ans = 1
	for i in range(h):
		for j in range(w):
			if M[i][j] == 1:
				ans = ans * 2 % mod
	return ans
mod = 1000000007
h_w = [int(__) for __ in input().split()]
h = h_w[0]
w = h_w[1]
M = [[1 for x in range(w)] for y in range(h)]
r = [int(ri) for ri in input().split()]
c = [int(cj) for cj in input().split()]
print(fill(M, r, c, h, w, mod))


(h, w) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
matrix = [[0] * w for i in range(h)]
for i in range(len(r)):
	for j in range(r[i]):
		matrix[i][j] = 1
for i in range(len(c)):
	for j in range(c[i]):
		matrix[j][i] = 1
for i in range(h):
	count = 0
	for j in range(w):
		if matrix[i][j] == 1:
			count = count + 1
		else:
			break
	if count > r[i]:
		print(0)
		exit()
for i in range(w):
	count = 0
	for j in range(h):
		if matrix[j][i] == 1:
			count = count + 1
		else:
			break
	if count > c[i]:
		print(0)
		exit()
s = set({})
for i in range(1, h):
	for j in range(r[i] + 1, w):
		if matrix[i][j] == 0 and c[j] < i:
			s.add((i, j))
print(2 ** len(s) % (10 ** 9 + 7))


import sys
from math import *
(r, c) = map(int, input().split())
maxi = 1000000007
a = [[0 for i in range(c)] for j in range(r)]
row = [int(x) for x in input().split()]
col = [int(x) for x in input().split()]
for i in range(r):
	for j in range(row[i]):
		a[i][j] = 1
for i in range(c):
	for j in range(col[i]):
		a[j][i] = 1
for i in range(r):
	f = 0
	j = 0
	while j < c and a[i][j] == 1:
		f += 1
		j += 1
	if f != row[i]:
		print(0)
		sys.exit()
for j in range(c):
	f = 0
	i = 0
	while i < r and a[i][j] == 1:
		f += 1
		i += 1
	if f != col[j]:
		print(0)
		sys.exit()
l = 1
for i in range(0, r):
	for j in range(0, c):
		if -(row[i] - j) >= 1 and -(col[j] - i) >= 1:
			l = l * 2 % maxi
l = l % maxi
print(l)


from __future__ import print_function
from collections import Counter, OrderedDict
from itertools import permutations as perm
from fractions import Fraction
from collections import deque
from sys import stdin
from bisect import *
from heapq import *
import math
g = lambda : stdin.readline().strip()
gl = lambda : g().split()
gil = lambda : [int(var) for var in gl()]
gfl = lambda : [float(var) for var in gl()]
gcl = lambda : list(g())
gbs = lambda : [int(var) for var in g()]
mod = int(1000000000.0) + 7
inf = float('inf')

def find_ge(a, x):
	i = bisect_left(a, x)
	if i != len(a):
		return a[i]
	return inf

def find_le(a, x):
	i = bisect_right(a, x)
	if i:
		return i - 1
	return -inf
(n, m) = gil()
(row, col) = (gil(), gil())
mat = [[2 for _ in range(m)] for _ in range(n)]
for r in range(n):
	for c in range(row[r]):
		if mat[r][c] == 0:
			print(0)
			exit()
		mat[r][c] = 1
	if row[r] < m:
		if mat[r][row[r]] == 1:
			print(0)
			exit()
		mat[r][row[r]] = 0
for c in range(m):
	for r in range(col[c]):
		if mat[r][c] == 0:
			print(0)
			exit()
		mat[r][c] = 1
	if col[c] < n:
		if mat[col[c]][c] == 1:
			print(0)
			exit()
		mat[col[c]][c] = 0
p = 0
for r in mat:
	p += r.count(2)
ans = 2 ** p
ans %= mod
print(ans)


import math
(h, w) = [x for x in map(int, input().split())]
R = [x for x in map(int, input().split())]
C = [x for x in map(int, input().split())]
F = [[2 for i in range(w + 1)] for j in range(h + 1)]
for i in range(h):
	for j in range(R[i]):
		F[i][j] = 1
	F[i][R[i]] = 0
for i in range(w):
	for j in range(C[i]):
		if F[j][i] == 0:
			F[j][i] = -1
		else:
			F[j][i] = 1
	if F[C[i]][i] == 1:
		F[C[i]][i] = -1
	else:
		F[C[i]][i] = 0
res = 0
flag = False
for i in range(h):
	for j in range(w):
		if F[i][j] == 2:
			res += 1
		if F[i][j] == -1:
			flag = True
			break
if flag:
	print(0)
else:
	print(2 ** res % (10 ** 9 + 7))


(h, w) = map(int, input().split())
R = [int(i) for i in input().split()]
C = [int(i) for i in input().split()]
T = [[0] * w for _ in range(h)]
ans = 1
for i in range(h):
	r = R[i]
	for j in range(r):
		T[i][j] = 1
	if r < w:
		T[i][r] = -1
for i in range(w):
	c = C[i]
	for j in range(c):
		if T[j][i] == 1:
			continue
		elif T[j][i] == -1:
			ans = 0
		else:
			T[j][i] = 1
	if c < h:
		if T[c][i] == 1:
			ans = 0
		elif T[c][i] == -1:
			continue
		else:
			T[c][i] = -1
ct = 0
for i in range(h):
	for j in range(w):
		if T[i][j] == 0:
			ct += 1
mod = 10 ** 9 + 7
print(pow(2, ct, mod) * ans)


(h, w) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
MOD = 10 ** 9 + 7
ch = 0
grid = [['?' for i in range(w + 1)] for j in range(h + 1)]
for i in range(h):
	for j in range(r[i]):
		if grid[i][j] == '?':
			grid[i][j] = 'FULL'
		elif grid[i][j] != 'FULL':
			ch = 1
	if grid[i][r[i]] == '?':
		grid[i][r[i]] = 'EMPTY'
	elif grid[i][r[i]] != 'EMPTY':
		ch = 1
for i in range(w):
	for j in range(c[i]):
		if grid[j][i] == '?':
			grid[j][i] = 'FULL'
		elif grid[j][i] != 'FULL':
			ch = 1
	if grid[c[i]][i] == '?':
		grid[c[i]][i] = 'EMPTY'
	if grid[c[i]][i] != 'EMPTY':
		ch = 1
ans = 0
for i in range(h):
	for j in range(w):
		if grid[i][j] == '?':
			ans += 1
if ch == 0:
	print(2 ** ans % MOD)
else:
	print(0)


(h, w) = map(int, input().split(' '))
rs = list(map(int, input().split(' ')))
cs = list(map(int, input().split(' ')))
x = [[0 for i in range(w)] for j in range(h)]
for i in range(h):
	for j in range(rs[i]):
		if x[i][j] == -1:
			print('0')
			exit()
		x[i][j] = 1
	if rs[i] < w:
		if x[i][rs[i]] == 1:
			print('0')
			exit()
		x[i][rs[i]] = -1
for i in range(w):
	for j in range(cs[i]):
		if x[j][i] == -1:
			print('0')
			exit()
		x[j][i] = 1
	if cs[i] < h:
		if x[cs[i]][i] == 1:
			print('0')
			exit()
		x[cs[i]][i] = -1
p = 0
for i in range(h):
	for j in range(w):
		if x[i][j] == 0:
			p += 1
print(str(2 ** p % 1000000007))


(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'
if error:
	print(0)
else:
	count = 0
	for i in arr:
		count += i.count('r')
	print(2 ** count % (10 ** 9 + 7))


import os
import sys
from io import BytesIO, IOBase

def main():
	(h, w) = map(int, input().split())
	r = list(map(int, input().split()))
	c = list(map(int, input().split()))
	a = [[0 for _ in range(w + 1)] for _ in range(h + 1)]
	for i in range(h):
		for j in range(r[i]):
			a[i][j] = 1
		a[i][r[i]] = -1
	for j in range(w):
		for i in range(c[j]):
			if a[i][j] == -1:
				print(0)
				return
			a[i][j] = 1
		if a[c[j]][j] == 1:
			print(0)
			return
		a[c[j]][j] = -1
	print(pow(2, sum(map(lambda x: x[:-1].count(0), a[:-1])), 10 ** 9 + 7))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


(q, w) = map(int, input().split())
e = list(map(int, input().split()))
r = list(map(int, input().split()))
k = 0
t = 0
for i in range(q):
	j = 0
	while j != e[i]:
		if i == r[j]:
			print(0)
			k = 1
			t = 1
			break
		j += 1
	if t == 1:
		break
l = 0
for i in range(w):
	j = 0
	while j != r[i]:
		if i == e[j]:
			if t == 0:
				print(0)
			k = 1
			l = 1
			break
		j += 1
	if l == 1:
		break
e = list(map(lambda x: min(int(x) + 1, w), e))
r = list(map(lambda x: min(int(x) + 1, q), r))
if k == 0:
	for i in range(q):
		j = e[i]
		while j != w:
			if i + 1 > r[j]:
				k += 1
			j += 1
	print(2 ** k % 1000000007)


import sys, itertools, math, collections, random

def ia():
	return [int(i) for i in sys.stdin.readline().strip().split(' ')]

def ii():
	return int(sys.stdin.readline().strip())

def istr():
	return sys.stdin.readline().strip()
(h, w) = ia()
A = [[1] * w for _ in range(h)]
R = ia()
C = ia()
ok = True
for r in range(h):
	for i in range(R[r]):
		A[r][i] = 2
	if R[r] < w:
		A[r][R[r]] = 0
for c in range(w):
	for i in range(C[c]):
		if A[i][c] == 0:
			ok = False
		A[i][c] = 2
	if C[c] < h:
		if A[C[c]][c] == 2:
			ok = False
		A[C[c]][c] = 0
e = sum([row.count(1) for row in A])
if not ok:
	print(0)
else:
	print(pow(2, e, 1000000007))


mod = 1000000007

def fexp(x, y):
	ans = 1
	while y > 0:
		if y % 2 == 1:
			ans = ans * x % mod
		x = x * x % mod
		y //= 2
	return ans
(h, w) = map(int, input().split(' '))
r = list(map(int, input().split(' ')))
c = list(map(int, input().split(' ')))
fixed = [[0 for i in range(w)] for i in range(h)]
ok = True
for i in range(h):
	for j in range(r[i]):
		fixed[i][j] = 1
	if r[i] < w:
		fixed[i][r[i]] = -1
for i in range(w):
	for j in range(c[i]):
		if fixed[j][i] == -1:
			ok = False
		fixed[j][i] = 1
	if c[i] < h:
		if fixed[c[i]][i] == 1:
			ok = False
		fixed[c[i]][i] = -1
if not ok:
	print(0)
else:
	count = 0
	for i in range(h):
		for j in range(w):
			if fixed[i][j] == 0:
				count += 1
	print(fexp(2, count))


(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) = [int(x) for x in input().split()]
(over, count) = (1, 0)
vh = []
vw = []
vh = [int(x) for x in input().split()]
vw = [int(x) for x in input().split()]
mat = [[0 for x in range(w)] for y in range(h)]
for i in range(h):
	j = 0
	while (j < vh[i]) & over:
		if j >= w:
			print(0)
			over = 0
		else:
			mat[i][j] = 1
		j += 1
for i in range(w):
	j = 0
	while (j < vw[i]) & over:
		if j >= h:
			print(0)
			over = 0
		elif vh[j] == i:
			print(0)
			over = 0
		else:
			mat[j][i] = 1
		j += 1
	if (j < h) & over:
		if mat[j][i] == 1:
			print(0)
			over = 0
	j += 1
	while (j < h) & over:
		if (j > 0) & (i > 0) & (i > vh[j]):
			count += 1
		j += 1
if over == 1:
	print(2 ** count % (10 ** 9 + 7))


from itertools import count
(h, w) = tuple(map(int, input().split(' ')))
r = tuple(map(int, input().split(' ')))
c = tuple(map(int, input().split(' ')))
arr = [['*' for _ in range(w)] for _ in range(h)]
for (i, ri) in zip(count(), r):
	for j in range(ri + 1):
		if j < len(arr[i]):
			arr[i][j] = 1 if j < ri else 0
flag = True
for (j, ci) in zip(count(), c):
	for i in range(ci + 1):
		if i < len(arr):
			val = 1 if i < ci else 0
			if arr[i][j] != '*' and val != arr[i][j]:
				flag = False
				break
			else:
				arr[i][j] = val
	if not flag:
		break
if not flag:
	print(0)
else:
	count = 0
	for row in arr:
		for el in row:
			if el == '*':
				count += 1
	ans = 1
	for _ in range(count):
		ans *= 2
		ans = ans % 1000000007
	print(ans)


import bisect
import collections
import copy
import functools
import heapq
import itertools
import math
import random
import re
import sys
import time
import string
from typing import *
sys.setrecursionlimit(99999)
mod = 10 ** 9 + 7
(h, w) = map(int, input().split())
ah = list(map(int, input().split()))
ac = list(map(int, input().split()))
dp = [[0] * w for _ in range(h)]
for (i, c) in enumerate(ah):
	for j in range(c):
		dp[i][j] = 1
for (j, c) in enumerate(ac):
	for i in range(c):
		dp[i][j] = 1
b = True
for i in range(h):
	j = 0
	while j < w and dp[i][j] == 1:
		j += 1
	if j != ah[i]:
		b = False
		break
if not b:
	print(0)
else:
	for j in range(w):
		i = 0
		while i < h and dp[i][j] == 1:
			i += 1
		if i != ac[j]:
			b = False
			break
	if not b:
		print(0)
	else:
		k = 0
		for i in range(1, h):
			for j in range(1, w):
				if dp[i][j] == 0 and i > ac[j] and (j > ah[i]):
					k += 1
		print(pow(2, k, mod))


import sys
(h, w) = [int(i) for i in input().split()]
matriz = [[-1] * w for _ in range(h)]
r = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]
for (i, v) in enumerate(r):
	if v == 0:
		matriz[i][0] = 0
	else:
		for x in range(v):
			matriz[i][x] = 1
		if x + 1 < w:
			matriz[i][x + 1] = 0
for (i, v) in enumerate(c):
	if v == 0:
		if matriz[0][i] == 1:
			print(0)
			sys.exit()
		matriz[0][i] = 0
	else:
		for x in range(v):
			if matriz[x][i] == 0:
				print(0)
				sys.exit()
			matriz[x][i] = 1
		if x + 1 < h:
			if matriz[x + 1][i] == 1:
				print(0)
				sys.exit()
			matriz[x + 1][i] = 0
temp = 0
for i in matriz:
	temp += i.count(-1)
print(2 ** temp % (10 ** 9 + 7))


def power(x, y):
	if y == 0:
		return 1
	elif int(y % 2) == 0:
		return power(x, int(y / 2)) * power(x, int(y / 2))
	else:
		return x * power(x, int(y / 2)) * power(x, int(y / 2))
(h, w) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
a = []
count = 0
for i in range(h):
	b = [2] * w
	a.append(b)
for i in range(h):
	j = 0
	while j < r[i]:
		a[i][j] = 1
		j += 1
		count += 1
	if j < w:
		a[i][j] = 0
		count += 1
for i in range(w):
	j = 0
	q = 0
	while j < c[i]:
		if a[j][i] == 0:
			q = 1
			break
		elif a[j][i] == 2:
			a[j][i] = 1
			count += 1
		else:
			a[j][i] = 1
		j += 1
	if j < h:
		if a[j][i] == 1:
			q = 1
		elif a[j][i] == 2:
			a[j][i] = 0
			count += 1
		else:
			a[j][i] = 0
	if q == 1:
		break
if q == 1:
	print(0)
else:
	print(power(2, h * w - count) % 1000000007)


(h, w) = [int(i) for i in input().split()]
r = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]
count = 0
possible = True
for i in range(h):
	for j in range(w):
		if j <= r[i] or i <= c[j]:
			...
		else:
			count += 1
		if i == c[j] and j < r[i] or (j == r[i] and i < c[j]):
			possible = False
			break
print(2 ** count % 1000000007 if possible else 0)


def cal_cell(h, w, r, c):
	res = 0
	for (index, value) in enumerate(r):
		t = value
		if value < w:
			t += 1
		for j in range(w):
			if j < value and c[j] == index:
				return -1
			if j == value and c[j] > index:
				return -1
			if j > value and c[j] >= index:
				t += 1
		left = w - t
		if left < 0:
			return -1
		res += left
	return res
(h, w) = list(map(int, input().split()))
r = list(map(int, input().split()))
c = list(map(int, input().split()))
cell = cal_cell(h, w, r, c)
if cell == -1:
	print(0)
else:
	print(int(pow(2, cell, 1000000007)))


(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))


MOD = 1000000007

def powr(n, N):
	temp = 1
	while N > 0:
		if N % 2 != 0:
			temp = temp * n % MOD
		n = n * n % MOD
		N = N // 2
	return temp % MOD
temp = 0
(h, w) = map(int, input().split())
r = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]
L = []
for i in range(0, h):
	d = []
	for j in range(0, w):
		d.append(-1)
	L.append(d)
for i in range(0, len(r)):
	for j in range(0, min(r[i], w)):
		L[i][j] = 1
	if r[i] < w:
		L[i][r[i]] = 0
for i in range(0, len(c)):
	for j in range(0, min(c[i], h)):
		if L[j][i] == 0:
			temp = 1
			break
		L[j][i] = 1
	if c[i] < h:
		if L[c[i]][i] == 1:
			temp = 1
			break
		L[c[i]][i] = 0
	if temp == 1:
		break
tot = 0
for i in range(h):
	for j in range(w):
		if L[i][j] == -1:
			tot += 1
if temp == 1:
	print(0)
else:
	print(powr(2, tot) % MOD)


from sys import stdin
(h, w) = list(map(int, stdin.readline().split()))
matrix = [[-1 for x in range(w)] for y in range(h)]
row = list(map(int, stdin.readline().split()))
col = list(map(int, stdin.readline().split()))
for i in range(h):
	num = row[i]
	for j in range(num):
		matrix[i][j] = 1
	if num < w:
		matrix[i][num] = 0
for j in range(w):
	num = col[j]
	for i in range(num):
		if matrix[i][j] == 0:
			print(0)
			exit()
		else:
			matrix[i][j] = 1
	if num < h:
		if matrix[num][j] == 1:
			print(0)
			exit()
		else:
			matrix[num][j] = 0
count = 0
for i in range(h):
	for j in range(w):
		if matrix[i][j] == -1:
			count += 1
mod = 10 ** 9 + 7
print(pow(2, count, mod))


(h, w) = map(int, input().split())
r = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]

def modexp(x, y, p):
	res = 1
	while y > 0:
		if y & 1:
			res *= x
			res %= p
		y >>= 1
		x *= x
	return res % p
M = 10 ** 9 + 7
canuse = [[True] * w for i in range(h)]
flag = 1
for i in range(h):
	for j in range(r[i]):
		canuse[i][j] = False
	if r[i] < w:
		canuse[i][r[i]] = False
for j in range(w):
	for i in range(c[j]):
		canuse[i][j] = False
	if c[j] < h:
		canuse[c[j]][j] = False
exp = 0
for i in range(h):
	for j in range(w):
		exp += int(canuse[i][j])
for i in range(h):
	for j in range(w):
		if r[i] >= j + 1 and c[j] == i:
			flag = 0
for i in range(h):
	for j in range(w):
		if c[j] >= i + 1 and r[i] == j:
			flag = 0
if flag == 0:
	print(0)
else:
	print(modexp(2, exp, M))


(h, w) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
g = [[0 for _ in range(w)] for _ in range(h)]
for i in range(h):
	if r[i] != 0:
		for j in range(r[i]):
			g[i][j] = 1
for i in range(w):
	if c[i] != 0:
		for j in range(c[i]):
			g[j][i] = 1
rchange = []
for i in range(h):
	change = 0
	for j in range(w):
		if g[i][j] == 1:
			change += 1
		else:
			rchange.append(change)
			break
		if j == w - 1:
			rchange.append(change)
cchange = []
for i in range(w):
	change = 0
	for j in range(h):
		if g[j][i] == 1:
			change += 1
		else:
			cchange.append(change)
			break
		if j == h - 1:
			cchange.append(change)
ignore = 0
if r == rchange and c == cchange:
	ignore = 1
ans = 0
for i in range(h):
	for j in range(w):
		if g[i][j] == 0:
			ycnt = 1
			xcnt = 1
			ystate = 0
			xstate = 0
			while True:
				if j - xcnt < 0:
					break
				if g[i][j - xcnt] == 0:
					xstate = 1
					break
				xcnt += 1
			while True:
				if i - ycnt < 0:
					break
				if g[i - ycnt][j] == 0:
					ystate = 1
					break
				ycnt += 1
			if xstate == 1 and ystate == 1:
				ans += 1
if ignore == 0:
	print(0)
else:
	print(2 ** ans % 1000000007)


(n, m) = map(int, input().split())
row = list(map(int, input().split()))
column = list(map(int, input().split()))
res = [[0] * m for i in range(n)]
out = [[False] * m for i in range(n)]
for i in range(n):
	for j in range(row[i]):
		res[i][j] = 1
		out[i][j] = True
	if row[i] < m:
		out[i][row[i]] = True
for i in range(m):
	for j in range(column[i]):
		res[j][i] = 1
		out[j][i] = True
	if column[i] < n:
		out[column[i]][i] = True
val = 1
mm = 1000000007
lol = 1
for i in range(n):
	if row[i] < m:
		if res[i][row[i]]:
			lol = 0
for i in range(m):
	if column[i] < n:
		if res[column[i]][i]:
			lol = 0
for i in range(n):
	for j in range(m):
		if out[i][j] == False:
			val = val % mm * 2 % mm
if lol == 0:
	print(lol)
else:
	print(val % mm)


class CellType:
	FILLED = 1
	EMPTY = 2
	UNKNOWN = 3
(h, w) = map(int, input().split())
rs = list(map(int, input().split()))
cs = list(map(int, input().split()))

def fill_the_grid(h, w, rs, cs):
	grid = []
	for i in range(h):
		grid.append([CellType.UNKNOWN] * w)
	for i in range(h):
		r_i = rs[i]
		for j in range(r_i):
			grid[i][j] = CellType.FILLED
		if r_i < w:
			grid[i][r_i] = CellType.EMPTY
	for j in range(w):
		c_i = cs[j]
		for i in range(c_i):
			if grid[i][j] == CellType.EMPTY:
				return 0
			grid[i][j] = CellType.FILLED
		if c_i < h:
			if grid[c_i][j] == CellType.FILLED:
				return 0
			grid[c_i][j] = CellType.EMPTY
	total = 0
	for i in range(h):
		for j in range(w):
			if grid[i][j] == CellType.UNKNOWN:
				total += 1
	return 2 ** total % 1000000007
print(fill_the_grid(h, w, rs, cs))


import os
import sys
from io import BytesIO, IOBase
import math
from decimal import *
from collections import defaultdict, deque
import heapq
getcontext().prec = 25
MOD = pow(10, 9) + 7
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
(n, k) = map(int, input().split(' '))
l = [['?'] * k for i in range(n)]
x = list(map(int, input().split(' ')))
y = list(map(int, input().split(' ')))
ans = 1
for i in range(n):
	for j in range(x[i]):
		l[i][j] = '+'
	if x[i] < k:
		l[i][x[i]] = '-'
for i in range(k):
	for j in range(y[i]):
		if l[j][i] == '-':
			ans = 0
			break
		l[j][i] = '+'
	if y[i] == 0:
		if l[y[i]][i] == '+':
			ans = 0
			break
	if y[i] < n:
		l[y[i]][i] = '-'
for i in range(n):
	for j in range(x[i]):
		if l[i][j] == '-':
			ans = 0
			break
if ans:
	for i in l:
		for j in i:
			if j == '?':
				ans *= 2
				ans %= MOD
	print(ans)
else:
	print(ans)


mod = 1000000007
(a, b) = map(int, input().split())
dat = []
for i in range(a):
	dat.append([0] * b)
aa = list(map(int, input().split()))
bb = list(map(int, input().split()))
can = True
for i in range(a):
	for j in range(aa[i]):
		dat[i][j] = 1
	if aa[i] != b:
		dat[i][aa[i]] = 2
for i in range(b):
	for j in range(bb[i]):
		if dat[j][i] == 2:
			can = False
		dat[j][i] = 1
	if bb[i] != a:
		if dat[bb[i]][i] == 1:
			can = False
		dat[bb[i]][i] = 2
res = 1
import collections
for i in range(a):
	c = collections.Counter(dat[i])
	if 0 not in c:
		x = 0
	else:
		x = c[0]
	res *= 2 ** x
	res %= mod
if can is False:
	print(0)
else:
	print(res)


(h, w) = map(int, input().split())
r = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]
board = [0 for _ in range(h * w)]
conflict = False
for (index, ri) in enumerate(r):
	for i in range(ri):
		board[index * w + i] = 1
	if ri == 0:
		board[index * w] = -1
	elif ri != w:
		board[index * w + ri] = -1
for (index, ci) in enumerate(c):
	for j in range(ci):
		conflict = board[index + w * j] == -1 or conflict
		board[index + w * j] = 1
	if ci == 0:
		conflict = board[index] == 1 or conflict
		board[index] = -1
	elif ci != h:
		conflict = board[index + w * ci] == 1 or conflict
		board[index + w * ci] = -1
undecided_spots = sum([board[i] == 0 for i in range(h * w)])
if undecided_spots == 0 or conflict:
	result = 0 if conflict else 1
else:
	result = 2 ** undecided_spots % (10 ** 9 + 7)
print(result)


from collections import Counter
l = [int(i) for i in input().split(' ')]
h = l[0]
w = l[1]
matrix = [[0 for j in range(w)] for i in range(h)]
r = [int(i) for i in input().split(' ')]
c = [int(i) for i in input().split(' ')]
for i in range(h):
	if r[i] == 0:
		matrix[i][0] = -1
	else:
		for j in range(r[i]):
			matrix[i][j] = 1
		if r[i] < w:
			matrix[i][r[i]] = -1
flag = 0
for j in range(w):
	if c[j] == 0:
		if matrix[0][j] == 1:
			print(0)
			flag = 1
			break
		else:
			matrix[0][j] = -1
	else:
		for i in range(c[j]):
			if matrix[i][j] == -1:
				print(0)
				flag = 1
				break
			else:
				matrix[i][j] = 1
		if flag == 1:
			break
		if c[j] < h:
			if matrix[c[j]][j] == 1:
				print(0)
				flag = 1
				break
			matrix[c[j]][j] = -1
		if flag == 1:
			break
if flag == 0:
	c = 0
	for i in matrix:
		c += i.count(0)
	print(2 ** c % 1000000007)


__author__ = 'Esfandiar'
import sys
input = sys.stdin.readline
(n, m) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
color = [[0] * m for _ in range(n)]
res = n * m
for i in range(n):
	for k in range(r[i]):
		color[i][k] = 2
		res -= 1
	if r[i] != m:
		color[i][r[i]] = 1
		res -= 1
for i in range(m):
	for k in range(c[i]):
		if color[k][i] == 1:
			print(0)
			exit()
		res -= color[k][i] == 0
		color[k][i] = 2
	if c[i] != n:
		if color[c[i]][i] == 2:
			print(0)
			exit()
		res -= color[c[i]][i] == 0
		color[c[i]][i] = 1
print(2 ** res % 1000000007)


(n, m) = map(int, input().split())
l1 = list(map(int, input().split()))
l2 = list(map(int, input().split()))
c = 0
k = False
for i in range(n):
	for j in range(m):
		if l1[i] < j and l2[j] < i:
			c += 1
		if l1[i] == j and l2[j] > i or (l2[j] == i and l1[i] > j):
			k = True
			break
if k == 1:
	print(0)
else:
	print(pow(2, c, 1000000007))


R = lambda : map(int, input().split())
(h, w) = map(range, R())
r = (*R(), 0)
c = (*R(), 0)
print((pow(2, sum((i > c[j] and j > r[i] for i in h for j in w)), 10 ** 9 + 7), 0)[any((i < c[r[i]] for i in h)) | any((i < r[c[i]] for i in w))])


def solve():
	(h, w) = (int(i) for i in input().split())
	h_i = [int(i) for i in input().split()]
	w_i = [int(i) for i in input().split()]
	arr = [[-1] * w for _ in range(h)]
	for i in range(h):
		for j in range(w):
			if j > h_i[i]:
				arr[i][j] = -1
			elif j == h_i[i]:
				arr[i][j] = 0
			else:
				arr[i][j] = 1
	for j in range(w):
		for i in range(h):
			if i > w_i[j]:
				pass
			elif i == w_i[j]:
				if arr[i][j] == 1:
					print(0)
					return
				arr[i][j] = 0
			else:
				if arr[i][j] == 0:
					print(0)
					return
				arr[i][j] = 1
	s = 0
	for j in range(w):
		for i in range(h):
			if arr[i][j] == -1:
				s += 1
	print(2 ** s % 1000000007)
solve()


import sys
import math
import bisect
import itertools

def solve(row, col):
	n = len(row)
	m = len(col)
	mod = 10 ** 9 + 7
	A = []
	for i in range(n):
		A.append([-1] * m)
	for i in range(n):
		for j in range(row[i]):
			A[i][j] = 1
		if row[i] < m:
			A[i][row[i]] = 0
	for i in range(m):
		for j in range(col[i]):
			if A[j][i] == 0:
				return 0
			A[j][i] = 1
		if col[i] < n:
			if A[col[i]][i] == 1:
				return 0
			A[col[i]][i] = 0
	ans = 1
	for i in range(n):
		for j in range(m):
			if A[i][j] == -1:
				ans = ans * 2 % mod
	return ans

def main():
	(n, m) = map(int, input().split())
	row = list(map(int, input().split()))
	col = list(map(int, input().split()))
	ans = solve(row, col)
	print(ans)
main()


(h, w) = input().split()
(h, w) = (int(h), int(w))
ar = list(map(int, input().split()))
br = list(map(int, input().split()))
m = 1000000007
flag = 0
visit = [[0 for x in range(w)] for y in range(h)]
for i in range(h):
	for j in range(ar[i] + 1):
		try:
			if j == ar[i]:
				visit[i][j] = 2
			else:
				visit[i][j] = 1
		except:
			nigg = 1
for i in range(w):
	for j in range(br[i] + 1):
		try:
			if j == br[i]:
				if visit[j][i] != 1:
					visit[j][i] = 2
				else:
					flag = -1
					break
			elif visit[j][i] != 2:
				visit[j][i] = 1
			else:
				flag = -1
				break
		except:
			nigg = 1
sum1 = 0
for i in visit:
	sum1 += i.count(0)
if flag == -1:
	print(0)
else:
	res = pow(sum1, 1, m - 1)
	ans = pow(2, res, m)
	print(ans)


R = lambda : map(int, input().split())
(h, w) = R()
r = list(R())
c = list(R())
grid = [[0 for i in range(w + 1)] for j in range(h + 1)]

def fill(row, col, value):
	if grid[row][col] == 0:
		grid[row][col] = value
	elif grid[row][col] != value:
		raise ValueError

def main():
	try:
		for i in range(h):
			for j in range(r[i]):
				fill(i, j, 'F')
			fill(i, r[i], 'E')
		for j in range(w):
			for i in range(c[j]):
				fill(i, j, 'F')
			fill(c[j], j, 'E')
	except ValueError:
		return 0
	ans = 1
	for i in range(h):
		for j in range(w):
			if grid[i][j] == 0:
				ans = ans * 2 % 1000000007
	return ans
print(main())


import sys
(h, w) = [int(x) for x in input().split()]
r = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]
MOD = 1000000007
UNKNOWN = 0
WHITE = 1
BLACK = 2
grid = [[UNKNOWN for _ in range(w)] for _ in range(h)]
for (i, ri) in enumerate(r):
	for x in range(ri):
		grid[i][x] = BLACK
	if ri < w:
		grid[i][ri] = WHITE
for (i, ci) in enumerate(c):
	for x in range(ci):
		if grid[x][i] == UNKNOWN:
			grid[x][i] = BLACK
		elif grid[x][i] == BLACK:
			continue
		elif grid[x][i] == WHITE:
			print(0)
			sys.exit(0)
	if ci < h:
		if grid[ci][i] == UNKNOWN:
			grid[ci][i] = WHITE
		elif grid[ci][i] == WHITE:
			continue
		elif grid[ci][i] == BLACK:
			print(0)
			sys.exit(0)
unknowns = sum((sum((int(x == UNKNOWN) for x in Y)) for Y in grid))
print(pow(2, unknowns, MOD))


def solve(h, w, r, c):
	ans = 0
	for i in range(h):
		p = r[i]
		for j in range(w):
			q = c[j]
			f = -3
			g = -3
			if p == j:
				f = 0
			elif j > p:
				f = -3
			elif j < p:
				f = 1
			if q == i:
				g = 0
			elif i > q:
				g = -3
			elif i < q:
				g = 1
			if f == 0 and g == 1 or (g == 0 and f == 1):
				return 0
			elif f == g == -3:
				ans += 1
	return 2 ** ans % 1000000007
(h, w) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
print(solve(h, w, r, c))


M = 10 ** 9 + 7
(h, w) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
ans = 1
mat = []
for i in range(h + 1):
	o = [0] * (w + 1)
	mat.append(o)
for row in range(h):
	col = r[row]
	for x in range(col):
		if mat[row][x] == 0:
			mat[row][x] = 1
	mat[row][col] = 2
for col in range(w):
	row = c[col]
	for x in range(row):
		if mat[x][col] == 2:
			print(0)
			exit()
		else:
			mat[x][col] = 1
	if mat[row][col] == 1:
		print(0)
		exit()
	else:
		mat[row][col] = 2
for row in range(h):
	for col in range(w):
		if mat[row][col] == 0:
			ans = ans * 2 % M
print(ans)


(h, w) = 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)


(h, w) = map(int, input().split())
mod = pow(10, 9) + 7
mat = dict()
result = True
for i in range(h):
	mat[i] = dict()
	for j in range(w):
		mat[i][j] = 0
l = list(map(int, input().split()))
r = list(map(int, input().split()))
for i in range(h):
	for j in range(l[i]):
		mat[i][j] = 1
for i in range(w):
	for j in range(r[i]):
		mat[j][i] = 1
	if r[i] < h:
		if mat[r[i]][i] == 1:
			result = False
			break
for i in range(h):
	if l[i] < w:
		if mat[i][l[i]] == 1:
			result = False
			break
if result:
	row = [0] * h
	col = [0] * w
	ans = 0
	for i in range(h):
		for j in range(w):
			if mat[i][j] == 0 and col[j] > 0 and (row[i] > 0):
				ans += 1
			if mat[i][j] == 0:
				row[i] += 1
				col[j] += 1
	print(pow(2, ans, mod))
else:
	print(0)


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
for i in range(w):
	for y in range(ls[i]):
		if pole[y][i] == -1:
			print(0)
			exit()
		else:
			pole[y][i] = 1
	if ls[i] != h:
		if pole[ls[i]][i] == 1:
			print(0)
			exit()
		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) = map(int, input().split())
r1 = list(map(int, input().split()))
c1 = list(map(int, input().split()))
l = []
for i in range(h):
	ka = [0] * w
	l.append(ka)
for i in range(len(r1)):
	for j in range(r1[i]):
		l[i][j] = -1
for i in range(len(c1)):
	for j in range(c1[i]):
		l[j][i] = -1
mod = 10 ** 9 + 7
ans = 0
for i in range(h):
	count = 0
	for k in range(len(l[i])):
		if l[i][k] != -1:
			break
		else:
			count += 1
	if count != r1[i]:
		print(0)
		exit()
	for j in range(k + 1, len(l[i])):
		if l[i][j] != -1:
			l[i][j] += 1
for i in range(w):
	count = 0
	for k in range(h):
		if l[k][i] != -1:
			break
		else:
			count += 1
	if count != c1[i]:
		print(0)
		exit()
	for j in range(k + 1, h):
		if l[j][i] == 1 and l[j][i] != -1:
			l[j][i] += 1
for i in range(h):
	for j in range(w):
		if l[i][j] == 2:
			ans += 1
print(pow(2, ans, mod))


(h, w) = input().split()
h = int(h)
w = int(w)
H = input().split()
r = []
c = []
valid = True
for x in H:
	r.append(int(x))
r.append(0)
W = input().split()
for x in W:
	c.append(int(x))
c.append(0)
ans = 1
emptySpace = 0
for i in range(0, len(r) - 1):
	now = 0
	if c[r[i]] == i + 1:
		valid = False
	for j in range(0, r[i]):
		if i == c[j]:
			valid = False
	for j in range(r[i] + 1, len(c) - 1):
		if c[j] < i:
			now += 1
	emptySpace += now
if valid:
	print(pow(2, emptySpace, 1000000007))
else:
	print(0)


(rows, cols) = [int(x) for x in input().split(' ')]
arr = [['BW'] * cols for x in range(rows)]
rowInput = [int(x) for x in input().split(' ')]
colInput = [int(x) for x in input().split(' ')]
flag = True
mod = 1000000007
for r in range(rows):
	t = rowInput[r]
	i = 0
	while i < t:
		arr[r][i] = 'B'
		i += 1
	if i < cols:
		arr[r][i] = 'W'
for c in range(cols):
	t = colInput[c]
	i = 0
	while i < t:
		if arr[i][c] == 'W':
			flag = False
			break
		arr[i][c] = 'B'
		i += 1
	if i < rows:
		if arr[i][c] == 'B':
			flag = False
			break
		arr[i][c] = 'W'
if flag:
	ans = 1
	for r in arr:
		for item in r:
			if item == 'BW':
				ans = ans * 2 % mod
	print(ans % mod)
else:
	print(0)


import sys, math, itertools
from collections import Counter, deque, defaultdict
from bisect import bisect_left, bisect_right
from heapq import heappop, heappush, heapify
mod = 10 ** 9 + 7
INF = float('inf')

def inp():
	return int(sys.stdin.readline())

def inpl():
	return list(map(int, sys.stdin.readline().split()))
(h, w) = inpl()
r = inpl()
c = inpl()
cnt = 0
for y in range(h):
	for x in range(w):
		if r[y] == x and c[x] > y:
			print(0)
			quit()
		if c[x] == y and r[y] > x:
			print(0)
			quit()
		if x > r[y] and y > c[x]:
			cnt += 1
print(pow(2, cnt, mod))


(h, w) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
squares = h * w
known = 0
possible = True
for i in range(h):
	known += r[i]
	if r[i] != w:
		known += 1
	for j in range(r[i]):
		if c[j] == i:
			possible = False
			break
for i in range(w):
	known += c[i]
	aux = 0
	if c[i] != h:
		known += 1
		aux = 1
	for j in range(c[i]):
		if r[j] == i:
			possible = False
			break
	for j in range(c[i] + aux):
		if r[j] >= i:
			known -= 1
if possible:
	print(2 ** (squares - known) % 1000000007)
else:
	print(0)


(h, w) = map(int, input().split())
num = [w * [0] for i in range(h)]
rows = list(map(int, input().split()))
cols = list(map(int, input().split()))
for i in range(h):
	for j in range(rows[i]):
		num[i][j] = 1
for j in range(w):
	for i in range(cols[j]):
		num[i][j] = 1
for i in range(h):
	k = 0
	while k < w and num[i][k] == 1:
		k += 1
	if k != rows[i]:
		print(0)
		exit(0)
for j in range(w):
	k = 0
	while k < h and num[k][j] == 1:
		k += 1
	if k != cols[j]:
		print(0)
		exit(0)
s = 0
for i in range(h):
	for j in range(w):
		if j > rows[i] and i > cols[j] and (num[i][j] == 0):
			s += 1
print(pow(2, s, 1000000007))


(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))


(h, w) = [int(c) for c in input().split()]
rows = [int(c) for c in input().split()]
cols = [int(c) for c in input().split()]

def main():
	dont_cares = 0
	for y in range(h):
		for x in range(w):
			if x < rows[y]:
				if cols[x] == y:
					return -1
			if x == rows[y]:
				if cols[x] >= y + 1:
					return -1
			if x > rows[y]:
				if cols[x] < y:
					dont_cares += 1
	return dont_cares
out = main()
if out == -1:
	out = 0
else:
	out = 2 ** out % (10 ** 9 + 7)
print(out)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
mod2 = 998244353
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

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

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def JA(a, sep):
	return sep.join(map(str, a))

def JAA(a, s, t):
	return s.join((t.join(map(str, b)) for b in a))

def IF(c, t, f):
	return t if c else f

def YES(c):
	return IF(c, 'YES', 'NO')

def Yes(c):
	return IF(c, 'Yes', 'No')

def main():
	t = 1
	rr = []
	for _ in range(t):
		(h, w) = LI()
		r = LI()
		c = LI()
		t = [[-1] * w for _ in range(h)]
		for i in range(h):
			for j in range(r[i]):
				t[i][j] = 1
			if r[i] < w:
				t[i][r[i]] = 0
		ok = 1
		for i in range(w):
			for j in range(c[i]):
				if t[j][i] == 0:
					ok = 0
					break
				t[j][i] = 1
			if c[i] < h:
				if t[c[i]][i] == 1:
					ok = 0
					break
				t[c[i]][i] = 1
		u = 0
		for ti in t:
			for tj in ti:
				if tj == -1:
					u += 1
		rr.append(IF(ok, pow(2, u, mod), 0))
	return JA(rr, '\n')
print(main())


(h, w) = map(int, input().split())
hs = list(map(int, input().split()))
ws = list(map(int, input().split()))
modulo = 10 ** 9 + 7
c = 0
ck = False
for i in range(h):
	for j in range(w):
		if hs[i] < j and ws[j] < i:
			c += 1
		if hs[i] == j and ws[j] > i or (ws[j] == i and hs[i] > j):
			ck = True
if ck:
	print(0)
else:
	print(2 ** c % modulo if c != 0 else 1)


def out():
	print(0)
	exit(0)
(n, m) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
res = [[-1] * m for _ in range(n)]
for i in range(n):
	for j in range(r[i]):
		res[i][j] = 1
	if r[i] < m:
		res[i][r[i]] = 0
for j in range(m):
	for i in range(c[j]):
		if not res[i][j]:
			out()
		res[i][j] = 1
	if c[j] < n:
		if res[c[j]][j] == 1:
			out()
		else:
			res[c[j]][j] = 0
print(2 ** sum([i.count(-1) for i in res]) % int(1000000000.0 + 7))


(h, w) = map(int, input().split(' '))
r = list(map(int, input().split(' ')))
c = list(map(int, input().split(' ')))
grid = [[0] * w for _ in range(h)]
s = h * w
invalid = False
for i in range(h):
	j = 0
	while j < r[i]:
		if grid[i][j] == 0:
			grid[i][j] = 1
			s -= 1
		j += 1
	if j < w:
		if grid[i][j] == 0:
			grid[i][j] = 2
			s -= 1
		elif grid[i][j] == 1:
			invalid = True
			break
if not invalid:
	for j in range(w):
		i = 0
		while i < c[j]:
			if grid[i][j] == 0:
				grid[i][j] = 1
				s -= 1
			elif grid[i][j] == 2:
				invalid = True
				break
			i += 1
		if not invalid and i < h:
			if grid[i][j] == 0:
				grid[i][j] = 2
				s -= 1
			elif grid[i][j] == 1:
				invalid = True
				break
		if invalid:
			break
q = 1000000007
if invalid:
	print(0)
elif s == 0:
	print(1)
else:
	ans = 1
	for i in range(s):
		ans = ans * 2 % q
	print(ans)


(n, m) = map(int, input().split())
b = [0] * 1001
a = [0] * 1001
N = 1000000007
a = list(map(int, input().split()))
b = list(map(int, input().split()))
sum = 0
ok = False
for i in range(n):
	for j in range(m):
		if a[i] < j and b[j] < i:
			sum += 1
		elif a[i] == j and b[j] > i or (b[j] == i and a[i] > j):
			ok = True
if ok == 1:
	print(0)
else:
	print(pow(2, sum, N))


(h, w) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
count = 0
for i in range(h):
	for j in range(w):
		if j == r[i] and c[j] > i or (i == c[j] and r[i] > j):
			print(0)
			exit()
		if j > r[i] and i > c[j]:
			count += 1
res = 2 ** count
print(res % (10 ** 9 + 7))


(h, w) = map(int, input().split())
(*r,) = map(int, input().split())
(*c,) = map(int, input().split())
s = h * w
minc = [0] * w
for i in range(0, h):
	s -= r[i]
	if r[i] != w:
		s -= 1
	if r[i] < w and c[r[i]] >= i + 1:
		print(0)
		exit()
	for j in range(w):
		if j < r[i] and minc[j] != -1:
			minc[j] += 1
		if j < r[i] and minc[j] != -1 and (c[j] < minc[j]):
			print(0)
			exit()
		if j >= r[i]:
			minc[j] = -1
		if j > r[i] and (c[j] >= i + 1 or c[j] - i == 0):
			s -= 1
print(pow(2, s, 10 ** 9 + 7))


(h, w) = [int(x) for x in input().split()]
harr = [int(j) for j in input().split()]
warr = [int(j) for j in input().split()]
mat = []
for i in range(h):
	tmp = []
	for j in range(w):
		tmp.append([0, 0])
	mat.append(tmp)
ans = 1
for i in range(h):
	for j in range(w):
		if i == warr[j] and j < harr[i] or (j == harr[i] and i < warr[j]):
			ans = 0
		if i >= warr[j]:
			if i != 0:
				mat[i][j][0] = mat[i - 1][j][0] + 1
			else:
				mat[i][j][0] = 1
		if j >= harr[i]:
			if j != 0:
				mat[i][j][1] = mat[i][j - 1][1] + 1
			else:
				mat[i][j][1] = 1
p2 = 0
for x in mat:
	for y in x:
		if min(y[0], y[1]) > 1:
			p2 += 1
while p2:
	p2 -= 1
	ans *= 2
	if ans > 1000000007:
		ans = ans % 1000000007
print(ans)


from sys import stdin
input = stdin.readline
mod = 1000000007
(h, w) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
arr = []
for i in range(h):
	arr.append([])
	for j in range(w):
		arr[i].append(0)
for i in range(h):
	for j in range(r[i]):
		arr[i][j] += 1
for i in range(w):
	for j in range(c[i]):
		arr[j][i] += 1
a = [0] * h
b = [0] * w
for i in range(h):
	j = 0
	while j < w and arr[i][j] != 0:
		j += 1
	a[i] = j
for i in range(w):
	j = 0
	while j < h and arr[j][i] != 0:
		j += 1
	b[i] = j
if a != r or c != b:
	print(0)
	exit()
cnt = 0
for i in range(1, h):
	for j in range(1, w):
		if r[i] < j and c[j] < i:
			cnt += 1
cnt = 1 << cnt
cnt %= mod
print(cnt)


(n, m) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
arr = [['#'] * (m + 2) for i in range(n + 2)]
for i in range(n):
	for j in range(r[i]):
		arr[i][j] = 'x'
	arr[i][r[i]] = '$'
check = True
for i in range(m):
	for j in range(c[i]):
		if arr[j][i] == '$':
			check = False
		arr[j][i] = 'x'
	if arr[c[i]][i] == 'x':
		check = False
	arr[c[i]][i] = '$'
tot = 0
for i in range(n):
	for j in range(m):
		if arr[i][j] == '#':
			tot += 1
if check == False:
	print(0)
else:
	print(2 ** tot % 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)


(r, c) = map(int, input().split())
lr = list(map(int, input().split()))
lc = list(map(int, input().split()))
mat = [[-1 for i in range(c)] for j in range(r)]
dupa = 0
for i in range(r):
	for j in range(lr[i]):
		if mat[i][j] != 0:
			mat[i][j] = 1
		else:
			dupa = 1
	if lr[i] < c:
		if mat[i][lr[i]] == 1:
			dupa = 1
		else:
			mat[i][lr[i]] = 0
for i in range(c):
	for j in range(lc[i]):
		if mat[j][i] != 0:
			mat[j][i] = 1
		else:
			dupa = 1
	if lc[i] < r:
		if mat[lc[i]][i] == 1:
			dupa = 1
		else:
			mat[lc[i]][i] = 0
if dupa == 1:
	print(0)
else:
	wyn = 0
	for i in range(r):
		for j in range(c):
			if mat[i][j] == -1:
				wyn += 1
	print(2 ** wyn % (10 ** 9 + 7))


mod = 10 ** 9 + 7
(h, w) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
grid = [[-1] * w for _ in range(h)]
ans = 1
for i in range(h):
	for j in range(r[i]):
		grid[i][j] = 1
	if r[i] < w:
		grid[i][r[i]] = 0
for i in range(w):
	for j in range(c[i]):
		if grid[j][i] == 0:
			ans = 0
		else:
			grid[j][i] = 1
	if c[i] < h:
		if grid[c[i]][i] == 1:
			ans = 0
		else:
			grid[c[i]][i] = 0
cnt = 0
for i in range(h):
	for j in range(w):
		if grid[i][j] == -1:
			cnt += 1
print(ans * pow(2, cnt, mod))


(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))


(r, c) = map(int, input().split())
rL = list(map(int, input().split()))
rC = list(map(int, input().split()))
mat = [[-1] * c for _ in range(r)]
for i in range(r):
	if rL[i] == 0:
		mat[i][0] = 1
	else:
		for j in range(rL[i]):
			mat[i][j] = 0
		if rL[i] < c:
			mat[i][rL[i]] = 1
flag = 0
for i in range(c):
	if rC[i] == 0:
		if mat[0][i] == 0:
			flag = 1
			break
		else:
			mat[0][i] = 1
	else:
		for j in range(rC[i]):
			if mat[j][i] == 1:
				flag = 1
				break
			else:
				mat[j][i] = 0
		if rC[i] < r:
			if mat[rC[i]][i] == 0:
				flag = 1
				break
			mat[rC[i]][i] = 1
count = 0
for i in mat:
	for j in i:
		if j == -1:
			count = count + 1
const = 1000000007
if flag == 1:
	print(0)
else:
	print(2 ** count % const)


from typing import List
EMPTY = 1
FULL = 2
MUST_BE_EMPTY = -1

def filling_the_grid(h: int, w: int, r: List[int], c: List[int]) -> int:
	cells = []
	for i in range(h):
		row = []
		for j in range(w):
			row.append(EMPTY)
		cells.append(row)
	num_of_undetermined_cells = w * h
	for i in range(h):
		num_of_full_cells = r[i]
		for j in range(num_of_full_cells):
			cells[i][j] = FULL
			num_of_undetermined_cells -= 1
		if num_of_full_cells < w:
			cells[i][num_of_full_cells] = MUST_BE_EMPTY
			num_of_undetermined_cells -= 1
	for j in range(w):
		num_of_full_cells = c[j]
		for i in range(num_of_full_cells):
			if cells[i][j] == MUST_BE_EMPTY:
				return 0
			if cells[i][j] != FULL:
				num_of_undetermined_cells -= 1
			cells[i][j] = FULL
		if num_of_full_cells < h:
			if cells[num_of_full_cells][j] == FULL:
				return 0
			if cells[num_of_full_cells][j] != MUST_BE_EMPTY:
				num_of_undetermined_cells -= 1
			cells[num_of_full_cells][j] = MUST_BE_EMPTY
	return 2 ** num_of_undetermined_cells % 1000000007

def test():
	assert filling_the_grid(3, 4, [0, 3, 1], [0, 2, 3, 0]) == 2
	assert filling_the_grid(1, 1, [0], [1]) == 0
	assert filling_the_grid(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]) == 797922655
(h, w) = list(map(int, input().split()))
r = list(map(int, input().split()))
c = list(map(int, input().split()))
answer_mod_one_million_and_seven = filling_the_grid(h, w, r, c)
print(answer_mod_one_million_and_seven)


def power(a, b, mod):
	res = 1
	while b:
		if b % 2:
			res = res * a % mod
		b //= 2
		a = a * a % mod
	return res % mod
(h, w) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
m = [[0] * w for i in range(h)]
for i in range(h):
	if r[i]:
		for j in range(r[i]):
			m[i][j] = 1
for i in range(w):
	if c[i]:
		for j in range(c[i]):
			m[j][i] = 1
ok = True
for i in range(h):
	if r[i] == 0:
		if m[i][0] == 1:
			ok = False
			break
	else:
		j = 0
		while j < w and m[i][j]:
			j += 1
		if j != r[i]:
			ok = False
			break
for j in range(w):
	if c[j] == 0:
		if m[0][j] == 1:
			ok = False
			break
	else:
		i = 0
		while i < h and m[i][j]:
			i += 1
		if i != c[j]:
			ok = False
			break
if ok:
	ct = 0
	for i in range(h):
		j = 0
		while j < w and m[i][j]:
			j += 1
		for k in range(j + 1, w):
			if m[i][k] == 0:
				m[i][k] = 2
				ct += 1
	for j in range(w):
		i = 0
		while i < h and m[i][j] == 1:
			i += 1
		if i != h and m[i][j] == 2:
			m[i][j] = 0
			ct -= 1
	print(power(2, ct, 1000000007))
else:
	print(0)


a = input('').split(' ')
h = int(a[0])
w = int(a[1])
r = list(map(int, input('').split(' ')))
c = list(map(int, input('').split(' ')))
g = []
gap = 0
for i in range(h):
	g.append([0] * w)
res = True
for i in range(h):
	for j in range(w):
		if j < r[i]:
			if i != c[j]:
				g[i][j] = 1
			else:
				res = False
				break
		else:
			break
	if not res:
		break
if res:
	for i in range(w):
		for j in range(h):
			if j < c[i]:
				if i != r[j]:
					g[j][i] = 1
				else:
					res = False
					break
			else:
				break
		if not res:
			break
for i in range(1, h, 1):
	for j in range(r[i] + 1, w, 1):
		if g[i][j] != 1:
			if i > c[j]:
				gap = gap + 1
M = 10 ** 9 + 7
if not res:
	print(0)
else:
	ans = pow(2, gap, M)
	print(ans)


import sys

def possibilities(height, width, r, c):
	board = []
	for i in range(height):
		board.append([0] * width)
	filled = 0
	for i in range(height):
		for j in range(r[i]):
			filled += 1
			board[i][j] = 1
		if r[i] < width:
			filled += 1
			board[i][r[i]] = -1
	for i in range(width):
		for j in range(c[i]):
			if board[j][i] == 0:
				filled += 1
				board[j][i] = 1
			elif board[j][i] == -1:
				print(0)
				return
		if c[i] < height:
			if board[c[i]][i] == 0:
				filled += 1
			elif board[c[i]][i] == 1:
				print(0)
				return
	flexible = height * width - filled
	ans = 1
	if flexible:
		for i in range(flexible):
			ans = ans * 2 % 1000000007
	print(ans)
(height, width) = map(int, sys.stdin.readline().split())
r = list(map(int, sys.stdin.readline().strip().split()))
c = list(map(int, sys.stdin.readline().strip().split()))
possibilities(height, width, r, c)


(r, c) = map(int, input().split())
(ri, ci) = (list(map(int, input().split())), list(map(int, input().split())))
l = []
fine = True
for _ in range(r):
	l.append([None] * c)
for i in range(r):
	if ri[i] == c:
		l[i] = [1] * c
	else:
		l[i][:ri[i] + 1] = [1] * ri[i] + [0]
for i in range(c):
	if ci[i] == r:
		for j in range(r):
			if l[j][i] == 0:
				fine = False
			l[j][i] = 1
	else:
		for j in range(ci[i]):
			if l[j][i] == 0:
				fine = False
			l[j][i] = 1
		if l[ci[i]][i] == 1:
			fine = False
		l[ci[i]][i] = 0
if not fine:
	print(0)
else:
	count = 0
	for i in range(r):
		for j in range(c):
			if l[i][j] == None:
				count += 1
	acc = 2
	rem = 1
	while count != 0:
		if count % 2 != 0:
			rem *= acc
			rem %= 1000000007
		acc *= acc
		acc %= 1000000007
		count //= 2
	print(rem)


(h, w) = map(int, input().split())
hlist = list(map(int, input().split()))
wlist = list(map(int, input().split()))
box = [[-1] * (w + 1) for i in range(h + 1)]
flag = 0
for x in range(h):
	y = 0
	while y < hlist[x]:
		box[x][y] = 1
		y += 1
	box[x][y] = 0
for y in range(w):
	x = 0
	while x < wlist[y] and flag == 0:
		if box[x][y] == 0:
			flag = 1
		box[x][y] = 1
		x += 1
	if box[x][y] == 1:
		flag = 1
	else:
		box[x][y] = 0
total = 0
for x in range(h):
	for y in range(w):
		if box[x][y] == -1:
			total += 1
if flag == 1:
	print(0)
else:
	print(2 ** total % (10 ** 9 + 7))


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
(h, w) = map(int, input().split())
lh = list(map(int, input().split()))
lw = list(map(int, input().split()))
flag = True
gr = []
MOD = 10 ** 9 + 7
for a in range(len(lh)):
	i = lh[a]
	end_ind = i - 1
	gr.append([1] * i + [0] * (w - i))
	if end_ind + 1 < w:
		gr[-1][end_ind + 1] = 2
for a in range(len(lw)):
	i = lw[a]
	end_ind = i - 1
	for k in range(i):
		if gr[k][a] == 1:
			pass
		elif gr[k][a] == 2:
			flag = False
			break
		else:
			gr[k][a] = 1
	if end_ind + 1 < h:
		if gr[end_ind + 1][a] == 1:
			flag = False
			break
		elif gr[end_ind + 1][a] == 2:
			pass
		else:
			gr[end_ind + 1][a] = 2
		gr[end_ind + 1][a] = 1
if flag:
	res = 0
	for y in range(h):
		for x in range(w):
			if gr[y][x] == 0:
				res += 1
				res %= MOD
	print(2 ** res % (10 ** 9 + 7))
else:
	print(0)


(h, w) = map(int, input().split())
matrix = [[0] * w for _ in range(h)]
row = list(map(int, input().split()))
col = list(map(int, input().split()))
cnt = 0
mod = 10 ** 9 + 7
for i in range(h):
	matrix[i][0:row[i]] = [1] * row[i]
	if row[i] < w:
		matrix[i][row[i]] = -1
for i in range(w):
	for j in range(col[i]):
		if matrix[j][i] != -1:
			matrix[j][i] = 1
		else:
			cnt = -1
			break
	if col[i] < h:
		if matrix[col[i]][i] != 1:
			matrix[col[i]][i] = -1
		else:
			cnt = -1
			break
if cnt == 0:
	for i in range(h):
		cnt += matrix[i].count(0)
	print(2 ** cnt % mod)
else:
	print(0)


def f(m, n, c1, c2):
	mat = [[-1 for i in range(n)] for j in range(m)]
	for i in range(m):
		for j in range(c1[i]):
			if mat[i][j] == 0:
				return -1
			mat[i][j] = 1
		if c1[i] < len(mat[i]):
			if mat[i][c1[i]] == 1:
				return -1
			mat[i][c1[i]] = 0
	for i in range(n):
		for j in range(c2[i]):
			if mat[j][i] == 0:
				return -1
			mat[j][i] = 1
		if c2[i] < m:
			if mat[c2[i]][i] == 1:
				return -1
			mat[c2[i]][i] = 0
	return mat
mn = list(map(int, input().split()))
m = mn[0]
n = mn[1]
c1 = list(map(int, input().split()))
c2 = list(map(int, input().split()))
temp = f(m, n, c1, c2)
if temp == -1:
	print(0)
else:
	countt = 0
	for i in temp:
		countt = countt + i.count(-1)
	print(2 ** countt % 1000000007)


(h, w) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
matrix = [[0 for x in range(w)] for y in range(h)]
for (index, i) in enumerate(r):
	for j in range(i):
		matrix[index][j] = 1
for (index, i) in enumerate(c):
	for j in range(i):
		matrix[j][index] = 1
flag = True
n = 0
for i in range(h):
	for j in range(w):
		if not flag:
			break
		if matrix[i][j] == 1:
			if r[i] == j or c[j] == i:
				print(0)
				flag = False
				break
		elif r[i] < j and c[j] < i:
			n += 1
mod = 1000000000.0 + 7
ans = 1
for _ in range(n):
	ans = ans * 2 % mod
if flag:
	print(int(ans))


def powe(a, b, mod):
	res = 1
	a = a % mod
	while b > 0:
		if b & 1 == 1:
			res = res * a % mod
		b = b >> 1
		a = a * a % mod
	return res
(h, w) = map(int, input().split())
ri = list(map(int, input().split()))
ci = list(map(int, input().split()))
const = [[0 for _ in range(w)] for _ in range(h)]
flag = True
for i in range(h):
	for j in range(ri[i] + 1):
		if j < ri[i]:
			const[i][j] = 1
		elif j < w:
			const[i][j] = -1
for i in range(w):
	for j in range(ci[i] + 1):
		if j < ci[i]:
			if const[j][i] != -1:
				const[j][i] = 1
			else:
				flag = False
				break
		elif j < h:
			if const[j][i] == 1:
				flag = False
				break
			const[j][i] = -1
	if flag == False:
		break
count_0 = 0
if flag == False:
	print(0)
else:
	for i in range(1, h):
		for j in range(1, w):
			if const[i][j] == 0:
				count_0 += 1
	print(powe(2, count_0, 1000000007))


(h, w) = list(map(int, input().split()))
r = list(map(int, input().split()))
c = list(map(int, input().split()))
m = [[-1] * w for i in range(h)]
flag = True
for i in range(h):
	if r[i] == 0:
		m[i][0] = 0
	else:
		for j in range(r[i]):
			m[i][j] = 1
		if r[i] < w:
			m[i][r[i]] = 0
for i in range(w):
	if c[i] == 0:
		if m[0][i] != 1:
			m[0][i] = 0
		else:
			flag = False
	else:
		for j in range(c[i]):
			if m[j][i] != 0:
				m[j][i] = 1
			else:
				flag = False
		if c[i] < h:
			if m[c[i]][i] != 1:
				m[c[i]][i] = 0
			else:
				flag = False
ans = 0
for i in range(h):
	for j in range(w):
		if m[i][j] == -1:
			ans += 1

def pos(x, n, m):
	if n == 0:
		return 1
	res = pos(x * x % m, n // 2, m)
	if n % 2 == 1:
		res = res * x % m
	return res
mod = 10 ** 9 + 7
if flag == True:
	print(pos(2, ans, mod))
else:
	print(0)


from sys import setcheckinterval, stdin, setrecursionlimit
setcheckinterval(1000)
setrecursionlimit(10 ** 7)

def iin():
	return int(stdin.readline())

def lin():
	return list(map(int, stdin.readline().split()))
(h, w) = lin()
r = lin()
c = lin()
md = 10 ** 9 + 7
mat = [[0 for i in range(w)] for j in range(h)]
ch = 0
for row in r:
	i = 0
	while i < row:
		mat[ch][i] = 2
		i += 1
	else:
		if i < w:
			mat[ch][i] = 1
	ch += 1
ch = 0
for col in c:
	i = 0
	while i < col:
		if mat[i][ch] == 1:
			print(0)
			exit()
		mat[i][ch] = 2
		i += 1
	else:
		if i < h:
			if mat[i][ch] <= 1:
				mat[i][ch] = 1
			else:
				print(0)
				exit()
	ch += 1
left = 0
for i in range(h):
	for j in range(w):
		if mat[i][j] == 0:
			left += 1
ans = 2 ** left
ans %= md
print(ans)


(h, w) = [int(i) for i in input().split()]
hs = [int(i) for i in input().split()]
ws = [int(i) for i in input().split()]
modulo = 10 ** 9 + 7
(count, flag) = (0, False)
for i in range(h):
	for j in range(w):
		if hs[i] < j and ws[j] < i:
			count += 1
		if hs[i] == j and ws[j] > i or (ws[j] == i and hs[i] > j):
			flag = True
if flag:
	print(0)
else:
	print(2 ** count % modulo if count != 0 else 1)


from collections import defaultdict
import sys
p2D = lambda x: print(*x, sep='\n')

def II():
	return int(sys.stdin.buffer.readline())

def MI():
	return map(int, sys.stdin.buffer.readline().split())

def LI():
	return list(map(int, sys.stdin.buffer.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def BI():
	return sys.stdin.buffer.readline().rstrip()

def SI():
	return sys.stdin.buffer.readline().rstrip().decode()

def li():
	return [int(i) for i in input().split()]

def lli(rows):
	return [li() for _ in range(rows)]

def si():
	return input()

def ii():
	return int(input())

def ins():
	return input().split()

def solve():
	mod = 10 ** 9 + 7
	(h, w) = li()
	r = li()
	c = li()
	grid = [[0] * w for _ in range(h)]
	for i in range(h):
		if r[i] > 0:
			for j in range(r[i]):
				grid[i][j] = 1
			if j + 1 < w:
				grid[i][j + 1] = -1
		else:
			grid[i][0] = -1
	flag = 0
	for i in range(w):
		if c[i] > 0:
			for j in range(c[i]):
				if grid[j][i] == -1:
					print(0)
					return
				grid[j][i] = 1
			if j + 1 < h:
				if grid[j + 1][i] == 1:
					print(0)
					return
				grid[j + 1][i] = -1
		else:
			if grid[0][i] == 1:
				print(0)
				return
			grid[0][i] = -1
	ans = 1
	for i in range(h):
		for j in range(w):
			if grid[i][j] == 0:
				ans = ans * 2 % mod
	print(ans)
	return

def main():
	solve()
main()


MOD = 10 ** 9 + 7
(h, w) = [int(item) for item in input().split()]
hs = [int(item) for item in input().split()]
ws = [int(item) for item in input().split()]
field = [[-1] * w for _ in range(h)]
for (i, hs) in enumerate(hs):
	for j in range(hs):
		field[i][j] = 1
	if hs < w:
		field[i][hs] = 0
for (i, ws) in enumerate(ws):
	for j in range(ws):
		if field[j][i] == 0:
			print(0)
			exit()
		else:
			field[j][i] = 1
	if ws < h:
		if field[ws][i] == 1:
			print(0)
			exit()
		else:
			field[ws][i] = 0
ans = 1
for line in field:
	for item in line:
		if item == -1:
			ans *= 2
			ans %= MOD
print(ans)


(n, m) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
grid = []
for i in range(n):
	temp = []
	for j in range(m):
		temp.append(-1)
	grid.append(temp)
for i in range(len(r)):
	for j in range(r[i]):
		grid[i][j] = 1
	if r[i] < m:
		grid[i][r[i]] = 0
possible = True
for j in range(len(c)):
	for i in range(c[j]):
		if grid[i][j] == 0:
			possible = False
			break
		else:
			grid[i][j] = 1
	if c[j] < n:
		if grid[c[j]][j] == 1:
			possible = False
		else:
			grid[c[j]][j] = 0
	if not possible:
		break
if not possible:
	print(0)
else:
	result = 0
	for i in range(n):
		for j in range(m):
			if grid[i][j] == -1:
				result += 1
	ans = 1
	for i in range(result):
		ans *= 2
		ans = int(ans % (1000000000.0 + 7))
	print(ans)


def main():
	(h, w) = map(int, input().split())
	r = list(map(int, input().split()))
	c = list(map(int, input().split()))
	total = 0
	for (rx, row) in enumerate(r):
		for cx in range(row):
			col = c[cx]
			if rx == col:
				print(0)
				return
		if row == w:
			continue
		cx = row
		col = c[cx]
		if col > rx:
			print(0)
			return
		for cx in range(row + 1, w):
			col = c[cx]
			if rx > col:
				total += 1
	total = 2 ** total
	total %= 1000000007
	print(total)
main()


from sys import stdin, stdout
from math import *
module = int(1000000000.0 + 7)

def main():
	global module
	(h, w) = [int(x) for x in stdin.readline().split()]
	r = [int(x) for x in stdin.readline().split()]
	c = [int(x) for x in stdin.readline().split()]
	a = [[None] * w for i in range(h)]
	try:
		for i in range(h):
			for j in range(w):
				if i <= c[j]:
					if i < c[j]:
						a[i][j] = 1
					else:
						a[i][j] = 0
				if j <= r[i]:
					if j < r[i]:
						if a[i][j] == None:
							a[i][j] = 1
						assert a[i][j] == 1
					else:
						if a[i][j] == None:
							a[i][j] = 0
						assert a[i][j] == 0
	except:
		stdout.write('0')
		return 0
	ways = 1
	for i in range(1, h):
		for j in range(1, w):
			if a[i][j] == None:
				ways = ways * 2 % module
	stdout.write(str(ways))
	return 0
main()


import sys
import os
from io import IOBase, BytesIO

def main():
	(n, m) = get_ints()
	r = get_array()
	c = get_array()
	mod = 10 ** 9 + 7
	arr = [[0 for j in range(m)] for i in range(n)]
	for i in range(n):
		for j in range(r[i]):
			arr[i][j] = 1
		if r[i] != m:
			arr[i][r[i]] = 2
	for i in range(m):
		for j in range(c[i]):
			if arr[j][i] == 2:
				print(0)
				return
			arr[j][i] = 1
		if c[i] != n:
			if arr[c[i]][i] == 1:
				print(0)
				return
			arr[c[i]][i] = 2
	cnt = 0
	for i in range(1, n):
		for j in range(1, m):
			if arr[i][j] == 0:
				cnt += 1
	cnt %= mod
	print(pow(2, cnt, mod) % mod)
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		py2 = round(0.5)
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2 == 1:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

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

def get_ints():
	return map(int, sys.stdin.readline().split())

def input():
	return sys.stdin.readline().strip()
main()


(h, w) = [int(z) for z in input().split()]
r = [int(z) for z in input().split()]
c = [int(z) for z in input().split()]

def validate(r: int, c: int, h: int, w: int):
	for i in range(h):
		if r[i] != w and c[r[i]] > i:
			return False
	for i in range(w):
		if c[i] != h and r[c[i]] > i:
			return False
	return True

def main(r: int, c: int, h: int, w: int):
	power = 0
	for i in range(1, w):
		tmp = h - (c[i] + 1)
		for j in range(c[i] + 1, h):
			if tmp <= 0:
				break
			if r[j] >= i:
				tmp -= 1
		if tmp > 0:
			power += tmp
	return power
if validate(r, c, h, w):
	print(pow(2, main(r, c, h, w), 10 ** 9 + 7))
else:
	print(0)


import sys, os, io

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

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

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math, datetime, functools, itertools, operator, bisect
from collections import deque, defaultdict, OrderedDict
import collections
N = 100009
factorialNumInverse = [None] * (N + 1)
naturalNumInverse = [None] * (N + 1)
fact = [None] * (N + 1)

def InverseofNumber(p):
	naturalNumInverse[0] = naturalNumInverse[1] = 1
	for i in range(2, N + 1, 1):
		naturalNumInverse[i] = naturalNumInverse[p % i] * (p - int(p / i)) % p

def InverseofFactorial(p):
	factorialNumInverse[0] = factorialNumInverse[1] = 1
	for i in range(2, N + 1, 1):
		factorialNumInverse[i] = naturalNumInverse[i] * factorialNumInverse[i - 1] % p

def factorial(p):
	fact[0] = 1
	for i in range(1, N + 1):
		fact[i] = fact[i - 1] * i % p

def Binomial(N, R, p):
	ans = fact[N] * factorialNumInverse[R] % p * factorialNumInverse[N - R] % p
	return ans

def main():
	starttime = datetime.datetime.now()
	if os.path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	p = 1000000007
	InverseofNumber(p)
	InverseofFactorial(p)
	factorial(p)
	for _ in range(1):
		(r, c) = ria()
		R = ria()
		C = ria()
		z = []
		ans = 1
		for i in range(r):
			z.append([0] * c)
		for i in range(r):
			k = R[i]
			for j in range(k):
				z[i][j] = 1
		for i in range(c):
			k = C[i]
			for j in range(k):
				z[j][i] = 1
		for i in range(c):
			k = C[i]
			t = 0
			j = 0
			while j < r and z[j][i]:
				t += 1
				j += 1
			if t != k:
				ans = 0
				break
		for i in range(r):
			k = R[i]
			t = 0
			j = 0
			while j < c and z[i][j]:
				t += 1
				j += 1
			if t != k:
				ans = 0
				break
		if ans == 0:
			print(0)
		else:
			for i in range(r):
				k = R[i]
				for j in range(k + 1, c):
					z[i][j] += 100
			for i in range(c):
				k = C[i]
				for j in range(k + 1, r):
					z[j][i] += 100
			count = 0
			for i in z:
				for j in i:
					if j == 200:
						count += 1
			for i in range(count):
				ans = ans * 2 % 1000000007
			print(ans)
	endtime = datetime.datetime.now()
	time = (endtime - starttime).total_seconds() * 1000
	if os.path.exists('input.txt'):
		print('Time:', time, 'ms')

class FastReader(io.IOBase):
	newlines = 0

	def __init__(self, fd, chunk_size=1024 * 8):
		self._fd = fd
		self._chunk_size = chunk_size
		self.buffer = io.BytesIO()

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self, size=-1):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

class FastWriter(io.IOBase):

	def __init__(self, fd):
		self._fd = fd
		self.buffer = io.BytesIO()
		self.write = self.buffer.write

	def flush(self):
		os.write(self._fd, self.buffer.getvalue())
		(self.buffer.truncate(0), self.buffer.seek(0))

class FastStdin(io.IOBase):

	def __init__(self, fd=0):
		self.buffer = FastReader(fd)
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

class FastStdout(io.IOBase):

	def __init__(self, fd=1):
		self.buffer = FastWriter(fd)
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.flush = self.buffer.flush
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()


def solve(h, w, r, c):
	ma = []
	for i in range(h):
		tmp = []
		for j in range(w):
			tmp.append(0)
		ma.append(tmp)
	for i in range(h):
		if r[i] > 0:
			for j in range(r[i]):
				ma[i][j] = 1
			if r[i] < w:
				ma[i][r[i]] = -1
		else:
			ma[i][0] = -1
	for i in range(w):
		if c[i] > 0:
			for j in range(c[i]):
				if ma[j][i] == -1:
					print('0')
					return
				ma[j][i] = 1
			if c[i] < h:
				if ma[c[i]][i] == 1:
					print('0')
					return
				ma[c[i]][i] = -1
		else:
			if ma[0][i] == 1:
				print('0')
				return
			ma[0][i] = -1
	ans = 1
	mod = 1000000007
	for i in range(h):
		for j in range(w):
			if ma[i][j] == 0:
				ans = ans * 2 % mod
	print(ans)
(h, w) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
solve(h, w, r, c)


pri = 10 ** 9 + 7
(a, b) = map(int, input().split())
row = list(map(int, input().split()))
col = list(map(int, input().split()))
count = 0
for i in range(len(row)):
	for j in range(len(col)):
		rno = i + 1
		cno = j + 1
		if cno > row[rno - 1] + 1 and rno > col[cno - 1] + 1:
			count += 1
		if row[i] < len(col):
			if col[row[i]] >= rno:
				print(0)
				exit()
		if col[j] < len(row):
			if row[col[j]] >= cno:
				print(0)
				exit()
if count == 0:
	print(1)
	exit()
print(pow(2, count, pri))


from sys import stdin, stdout
from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import *
from random import *
from itertools import permutations
input = stdin.readline
prin = stdout.write
from random import sample
from collections import Counter, deque
from fractions import *
from math import sqrt, ceil, log2, gcd, cos, pi, floor
from copy import deepcopy
mod = 10 ** 9 + 7
mod2 = 998244353

def ps(n):
	cp = 0
	lk = 0
	arr = {}
	lk = 0
	ap = n
	cc = 0
	while n % 2 == 0:
		n = n // 2
		cc = 1
	if cc == 1:
		lk += 1
	for ps in range(3, ceil(sqrt(n)) + 1, 2):
		cc = 0
		while n % ps == 0:
			n = n // ps
			cc = 1
		lk += 1 if cc == 1 else 0
	if n != 1:
		lk += 1
	if lk == 1:
		return False
	return True

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def factorials(n, r):
	slr = n
	dpr = r
	qlr = 1
	qs = 1
	mod = 10 ** 9 + 7
	for ip in range(n - r + 1, n):
		qlr = qlr * ip % mod
	for ij in range(1, r):
		qs = qs * ij % mod
	ans = qlr * modInverse(qs) % mod
	return ans

def modInverse(b):
	qr = 10 ** 9 + 7
	return pow(b, qr - 2, qr)
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func

def power(arr):
	listrep = arr
	subsets = []
	for i in range(2 ** len(listrep)):
		subset = []
		for k in range(len(listrep)):
			if i & 1 << k:
				subset.append(listrep[k])
		subsets.append(subset)
	return subsets

def pda(n):
	list = []
	su = 0
	for i in range(1, int(sqrt(n) + 1)):
		if n % i == 0:
			if n // i == i:
				list.append(i)
				su += i
			else:
				list.append(n // i)
				list.append(i)
				su += i
				su += n // i
	return su

def dis(xa, ya, xb, yb):
	return sqrt((xa - xb) ** 2 + (ya - yb) ** 2)

def ii():
	return int(input())

def ilist():
	return [int(x) for x in input().strip().split()]

def islist():
	return list(map(str, input().split().rstrip()))

def inp():
	return input().strip()

def google(test):
	return 'Case #' + str(test) + ': '

def overlap(x1, y1, x2, y2):
	if x2 > y1:
		return y1 - x2
	if y1 > y2:
		return y2 - x2
	return y1 - x2

def dist(x1, y1, x2, y2):
	return sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)

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

def prod(arr):
	n = len(arr)
	k = 1
	for j in range(n):
		k *= arr[j]
	return k

def SumOfDigits(s):
	su = 0
	while s:
		su += s % 10
		s = s // 10
	return su
t = 1
for pl in range(t):
	(h, w) = ilist()
	arr = [[-1] * w for i in range(h)]
	row = ilist()
	col = ilist()
	cc = 0
	for i in range(h):
		for j in range(row[i]):
			arr[i][j] = 1
		if row[i] < w:
			arr[i][row[i]] = 0
	for i in range(w):
		for j in range(col[i]):
			if arr[j][i] == -1:
				arr[j][i] = 1
			elif arr[j][i] != 1:
				cc = 1
		if col[i] < h:
			if arr[col[i]][i] == -1:
				arr[col[i]][i] = 0
			elif arr[col[i]][i] != 0:
				cc = 1
		if cc == 1:
			break
	if cc == 1:
		print(0)
		continue
	co = 0
	for i in range(h):
		for j in range(w):
			if arr[i][j] == -1:
				co += 1
	print(pow(2, co, mod))


(n, m) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
g = []
for i in range(n):
	g.append([1] * r[i])
	g[-1].extend([0] * (m - r[i]))
for i in range(m):
	for j in range(c[i]):
		g[j][i] = 1
for i in range(n):
	for j in range(m):
		if g[i][j] != 1:
			break
	else:
		if r[i] != j + 1:
			print(0)
			exit()
		continue
	if r[i] != j:
		print(0)
		exit()
for i in range(m):
	for j in range(n):
		if g[j][i] != 1:
			break
	else:
		if c[i] != j + 1:
			print(0)
			exit()
		continue
	if c[i] != j:
		print(0)
		exit()
x = 0
for i in range(n):
	for j in range(m):
		if g[i][j] == 0:
			if i > c[j] and j > r[i]:
				x += 1
print(pow(2, x, 7 + 10 ** 9))


(h, w) = map(int, input().split())
H = list(map(int, input().split()))
W = list(map(int, input().split()))
m = [[2] * w for _ in range(h)]
flag = 1
for i in range(h):
	if H[i] == 0:
		m[i][0] = 0
	else:
		for j in range(H[i]):
			m[i][j] = 1
		if H[i] < w:
			m[i][H[i]] = 0
for i in range(w):
	if W[i] == 0:
		if m[0][i] != 1:
			m[0][i] = 0
		else:
			flag = 0
			break
	else:
		for j in range(W[i]):
			if m[j][i] != 0:
				m[j][i] = 1
			else:
				flag = 0
				break
		if W[i] < h:
			if m[W[i]][i] != 1:
				m[W[i]][i] = 0
			else:
				flag = 0
				break
if flag != 0:
	res = 1
	for i in range(h):
		for j in range(w):
			if m[i][j] == 2:
				res *= 2
				res = res % 1000000007
	print(res)
else:
	print(0)


(a, b) = map(int, input().split())
if a == 0 or b == 0:
	print(1)
	exit()
r = list(map(int, input().split()))
c = list(map(int, input().split()))
board = [[-1 for _ in range(b)] for _ in range(a)]
for i in range(a):
	for j in range(r[i]):
		board[i][j] = 1
for j in range(b):
	for i in range(c[j]):
		board[i][j] = 1
for i in range(a):
	count = 0
	for j in range(b):
		if board[i][j] == 1:
			count += 1
		else:
			break
	if r[i] != count:
		print(0)
		exit()
for j in range(b):
	count = 0
	for i in range(a):
		if board[i][j] == 1:
			count += 1
		else:
			break
	if c[j] != count:
		print(0)
		exit()
count = 0
for i in range(1, a):
	for j in range(1, b):
		if board[i][j] == -1 and r[i] != j and (c[j] != i):
			count += 1
mod = int(1000000000.0 + 7)
cur = 1
for i in range(count):
	cur <<= 1
	cur %= mod
print(cur)


(h, w) = map(int, input().split(' '))
r = list(map(int, input().split(' ', h)[:h]))
c = list(map(int, input().split(' ', w)[:w]))
mat = [[0 for i in range(w + 1)] for j in range(h + 1)]
fl = 0
for i in range(h):
	for j in range(r[i]):
		if mat[i][j] == 0:
			mat[i][j] = 1
		elif mat[i][j] != 1:
			fl = 1
	if mat[i][r[i]] == 0:
		mat[i][r[i]] = 2
	elif mat[i][r[i]] != 2:
		fl = 1
for i in range(w):
	for j in range(c[i]):
		if mat[j][i] == 0:
			mat[j][i] = 1
		elif mat[j][i] != 1:
			fl = 1
	if mat[c[i]][i] == 0:
		mat[c[i]][i] = 2
	elif mat[c[i]][i] != 2:
		fl = 1
if fl == 1:
	print(0)
else:
	ans = 1
	for i in range(h):
		for j in range(w):
			if mat[i][j] == 0:
				ans *= 2
				ans %= 1000000007
	print(ans)


(h, w) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
x = [[0 for i in range(w)] for j in range(h)]
for i in range(h):
	for k in range(min(r[i], w)):
		x[i][k] = 1
for j in range(w):
	for k in range(min(c[j], h)):
		x[k][j] = 1
possible = 1
for i in range(h):
	t = 0
	for j in range(w):
		if x[i][j] == 0:
			break
		t += 1
	if t > r[i]:
		possible = 0
		break
for i in range(w):
	t = 0
	for j in range(h):
		if x[j][i] == 0:
			break
		t += 1
	if t > c[i]:
		possible = 0
		break
if possible == 0:
	print(0)
	exit()
x = [[0 for i in range(w)] for j in range(h)]
for i in range(h):
	for k in range(min(r[i] + 1, w)):
		x[i][k] = 1
for j in range(w):
	for k in range(min(c[j] + 1, h)):
		x[k][j] = 1
s = 0
for i in x:
	s += sum(i)
m = 10 ** 9 + 7
print(pow(2, h * w - s, m))


MOD = 1000000007

def main():
	(h, w) = list(map(int, input().split()))
	r = list(map(int, input().split()))
	c = list(map(int, input().split()))
	grid = [[2] * w for _ in range(h)]
	for i in range(h):
		for j in range(r[i]):
			grid[i][j] = 'b'
		if r[i] < w:
			grid[i][r[i]] = 'w'
	for i in range(w):
		for j in range(c[i]):
			if grid[j][i] == 'w':
				return 0
			grid[j][i] = 'b'
		if c[i] < h:
			if grid[c[i]][i] == 'b':
				return 0
			grid[c[i]][i] = 'w'
	ans = 1
	for row in grid:
		for c in row:
			if type(c) == int:
				ans = ans * c % MOD
	return ans
print(main())


ints = list(map(int, input().split()))
(h, w) = (ints[0], ints[1])
grid = []
r = list(map(int, input().split()))
c = list(map(int, input().split()))
for i in range(h):
	grid.append([0] * w)
	for rr in range(r[i]):
		if rr < w:
			grid[i][rr] = 1
	if r[i] < w:
		grid[i][r[i]] = -1
possible = True
for i in range(w):
	if not possible:
		break
	for cc in range(c[i] + 1):
		if cc < c[i]:
			if grid[cc][i] == -1:
				possible = False
				break
			grid[cc][i] = 1
	if c[i] < h and grid[c[i]][i] == 1:
		possible = False
		break
	elif c[i] < h:
		grid[c[i]][i] = -1
unreserved = 0
if possible:
	for i in range(h):
		for j in range(w):
			if grid[i][j] == 0:
				unreserved += 1
if unreserved == 0 and possible:
	print('1')
elif unreserved == 0 and (not possible):
	print('0')
else:
	print(2 ** unreserved % 1000000007)


if 1:
	(r, c) = map(int, input().split())
	a = [[0 for i in range(c)] for j in range(r)]
	R = list(map(int, input().split()))
	C = list(map(int, input().split()))
	flag = 1
	for i in range(r):
		for j in range(min(c, R[i] + 1)):
			a[i][j] = 1
			if j == R[i]:
				a[i][j] = 2
	for i in range(c):
		for j in range(min(r, C[i] + 1)):
			if j != C[i] and a[j][i] == 2:
				flag = 0
				break
			elif j == C[i] and a[j][i] == 1:
				flag = 0
				break
			a[j][i] = 1
		if flag == 0:
			break
	s = 0
	for i in range(r):
		for j in range(c):
			if a[i][j] == 0:
				s += 1
	ans = 2
	N = 1000000007

	def expo(bas, exp):
		if exp == 0:
			return 1
		if exp == 1:
			return bas % N
		t = expo(bas, int(exp / 2))
		t = t * t % N
		if exp % 2 == 0:
			return t
		else:
			return bas % N * t % N
	ans = expo(2, s)
	if flag == 0:
		print(0)
	else:
		print(ans)


(n, m) = map(int, input().split())
rows = list(map(int, input().split()))
columns = list(map(int, input().split()))
visited = [[0] * m for _ in range(n)]
for i in range(n):
	for j in range(min(m, rows[i])):
		visited[i][j] = 1
for j in range(m):
	for i in range(min(n, columns[j])):
		visited[i][j] = 1
import sys
for i in range(n):
	prev = 0
	for j in range(m):
		if visited[i][j] == 1:
			prev += 1
		else:
			break
	if prev != rows[i]:
		print(0)
		sys.exit()
for j in range(m):
	prev = 0
	for i in range(n):
		if visited[i][j] == 1:
			prev += 1
		else:
			break
	if prev != columns[j]:
		print(0)
		sys.exit()
for i in range(n):
	if rows[i] != m:
		visited[i][rows[i]] = 1
for j in range(m):
	if columns[j] != n:
		visited[columns[j]][j] = 1
temp = 0
for item in visited:
	temp += item.count(0)
print(pow(2, temp, 10 ** 9 + 7))


p = 10 ** 9 + 7
(h, w) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
grid = []
f = 0
x = 0
for i in range(h):
	l = list('1' * r[i] + '2' + '0' * (w - r[i] - 1))
	grid.append(l)
for i in range(w):
	if c[i] == 0 and grid[0][i] == '1':
		f = 1
		break
	elif c[i] == 0:
		grid[0][i] = '2'
		pass
	else:
		for j in range(0, c[i]):
			if j < c[i]:
				if r[j] != i:
					if grid[j][i] == '2':
						f = 1
						break
					grid[j][i] = '1'
				else:
					f = 1
					break
		if c[i] != h:
			if grid[c[i]][i] == '1':
				f = 1
				break
			grid[c[i]][i] = '2'
for i in range(h):
	q = e = 0
	for j in range(w):
		if grid[i][j] == '0':
			q += 1
	x += q
if f:
	print(0)
else:
	print(pow(2, x, p))


(h, w) = map(int, input().split())
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]:
				ans *= 2
				if ans > mod:
					ans -= mod
print(ans)


mod = 10 ** 9 + 7
from sys import stdin, stdout
import bisect
from bisect import bisect_left as bl
from bisect import bisect_right as br
import itertools
import collections
import math
import heapq
from random import randint as rn

def modinv(n, p):
	return pow(n, p - 2, p)

def ncr(n, r, p):
	t = fact[n] * (ifact[r] * ifact[n - r] % p) % p
	return t

def cin():
	return map(int, sin().split())

def ain():
	return list(map(int, sin().split()))

def sin():
	return input()

def inin():
	return int(input())

def GCD(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def main():
	(n, m) = cin()
	ans = -1
	a = []
	b = [0] * m
	for i in range(n):
		a.append(b[:])
	l = ain()
	mm = ain()
	for i in range(n):
		for j in range(l[i]):
			if j < m:
				a[i][j] = 1
	for i in range(m):
		for j in range(mm[i]):
			if j < n:
				a[j][i] = 1
	for i in range(n):
		if l[i] < m and a[i][l[i]] == 1:
			ans = 0
	for i in range(m):
		if mm[i] < n and a[mm[i]][i] == 1:
			ans = 0
	for i in range(n):
		j = l[i]
		if j < m:
			a[i][j] = 1
	for i in range(m):
		j = mm[i]
		if j < n:
			a[j][i] = 1
	if ans == 0:
		print(0)
		sys.exit()
	c = 0
	for i in range(n):
		for j in range(m):
			if a[i][j] == 0:
				c += 1
	print(pow(2, c, mod))
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


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(1000000000.0 + 7)
		ans = 1
		for _ in range(prob):
			ans = ans * 2 % mod
		print(ans)

def __starting_point():
	main()
__starting_point()


def do(h, w, r, c):
	ar = []
	for i in range(h):
		l = []
		for j in range(w):
			l.append(None)
		ar.append(l)
	for i in range(h):
		j = 0
		while j != r[i]:
			if ar[i][j] == None or ar[i][j] == True:
				ar[i][j] = True
			else:
				return 0
			j += 1
		if r[i] < w and ar[i][r[i]] == True:
			return 0
		if j < w:
			ar[i][j] = False
	for i in range(w):
		j = 0
		while j != c[i]:
			if ar[j][i] == None or ar[j][i] == True:
				ar[j][i] = True
			else:
				return 0
			j += 1
		if c[i] < h and ar[c[i]][i] == True:
			return 0
		if j < h:
			ar[j][i] = False
	ans = 1
	o = int(1000000000.0 + 7)
	for i in range(h):
		for j in range(w):
			if ar[i][j] == None:
				ans = ans * 2 % o
	return ans
(h, w) = [int(x) for x in input().split()]
r = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]
print(do(h, w, r, c))


import sys
input = sys.stdin.readline
from collections import defaultdict as dc
from bisect import bisect_right
import math
(n, m) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
l = [[-1 for j in range(m)] for i in range(n)]
f = 0
for i in range(n):
	for j in range(r[i]):
		l[i][j] = 1
	if r[i] != m:
		l[i][r[i]] = 0
for i in range(m):
	for j in range(c[i]):
		if l[j][i] == 0:
			f = 1
			break
		l[j][i] = 1
	if f:
		break
	if c[i] != n:
		if l[c[i]][i] == 1:
			f = 1
			break
		l[c[i]][i] = 0
if f:
	print(0)
else:
	c = 0
	for i in range(n):
		for j in range(m):
			if l[i][j] == -1:
				c += 1
	print(pow(2, c, pow(10, 9) + 7))


(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))


import sys
sys.setrecursionlimit(10000000)

def input():
	return sys.stdin.readline()[:-1]
from bisect import *
from collections import *
from heapq import *
import itertools
import math
INF = 10 ** 9
MOD = 10 ** 9 + 7
(h, w) = map(int, input().split())
r = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]
a = [[None] * w for i in range(h)]
for (i, x) in enumerate(r):
	for j in range(x):
		a[i][j] = 1
	if x < w:
		a[i][x] = 0
for (i, x) in enumerate(c):
	for j in range(x):
		if a[j][i] != 0:
			a[j][i] = 1
		else:
			print(0)
			exit()
	if x < h:
		if a[x][i] != 1:
			a[x][i] = 0
		else:
			print(0)
			exit()
cnt = 0
for i in range(h):
	cnt += a[i].count(None)
print(pow(2, cnt, MOD))


ch = input()
i = ch.index(' ')
n = int(ch[:i])
m = int(ch[i + 1:])
M = [[-1 for i in range(m)] for j in range(n)]
ch1 = input()
ch2 = input()
L1 = [int(i) for i in ch1.split()]
L2 = [int(i) for i in ch2.split()]
Test = True
for i in range(n):
	for j in range(0, L1[i]):
		M[i][j] = 1
	if L1[i] < m:
		M[i][L1[i]] = 0
for i in range(m):
	for j in range(0, L2[i]):
		if M[j][i] == 0:
			Test = False
		M[j][i] = 1
	if L2[i] < n:
		if M[L2[i]][i] == 1:
			Test = False
		M[L2[i]][i] = 0
nb = 0
for i in range(n):
	for j in range(m):
		if M[i][j] == -1:
			nb = nb + 1
x = 1
for i in range(nb):
	x = x * 2 % 1000000007
if Test == True:
	print(x)
else:
	print(0)


MOD = 1000000007

def fast_power(base, power):
	result = 1
	while power > 0:
		if power % 2 == 1:
			result = result * base % MOD
		power = power // 2
		base = base * base % MOD
	return result
(r, c) = map(int, input().split())
power = []
graph = []
count = 0
flag = 0
w = [int(x) for x in input().split()]
h = [int(x) for x in input().split()]
count = 0
for i in range(0, r):
	for j in range(0, c):
		if i - h[j] < 0 and j - w[i] == 0 or (j - w[i] < 0 and i - h[j] == 0):
			flag = 1
		elif i - h[j] >= 1 and j - w[i] >= 1:
			count += 1
if flag == 0:
	print(fast_power(2, count))
else:
	print(0)


_ = input()
r = list(map(int, input().split(' ')))
c = list(map(int, input().split(' ')))
arr = [[0] * len(c) for _ in range(len(r))]
impossible = False
for (i, x) in enumerate(r):
	for j in range(0, x):
		arr[i][j] = 1
	if x < len(c):
		arr[i][x] = -1
for (i, x) in enumerate(c):
	for j in range(0, x):
		if arr[j][i] == -1:
			impossible = True
		arr[j][i] = 1
	if x < len(r):
		if arr[x][i] == 1:
			impossible = True
		arr[x][i] = -1
s = 0
for row in arr:
	for x in row:
		s += x == 0
if impossible:
	print(0)
else:
	print((1 << s) % 1000000007)


(h, w) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
a = []
for i in range(h):
	t = [0] * w
	if r[i] < w:
		t[r[i]] = 1
	for j in range(r[i]):
		t[j] = 2
	a.append(t)
co = 1
for i in range(w):
	if c[i] < h:
		if a[c[i]][i] == 2:
			co = 0
			break
		else:
			a[c[i]][i] = 1
	for j in range(c[i]):
		if a[j][i] == 1:
			co = 0
			break
		else:
			a[j][i] = 2
	if co == 0:
		break
d = 0
for i in range(h):
	d = d + a[i].count(0)
if d == 0:
	kq = 1
else:
	kq = 2 ** d % 1000000007
if co == 0:
	kq = 0
print(kq)


import sys
sys.setrecursionlimit(10 ** 9)
input = sys.stdin.readline
(H, W) = map(int, input().split())
R = list(map(int, input().split()))
C = list(map(int, input().split()))
grid = [[None] * W for _ in range(H)]
for (i, r) in enumerate(R):
	for j in range(r):
		grid[i][j] = 'B'
	if r < W:
		grid[i][r] = 'W'
for (i, c) in enumerate(C):
	for j in range(c):
		if grid[j][i] == 'W':
			print(0)
			exit()
		grid[j][i] = 'B'
	if c < H:
		if grid[c][i] == 'B':
			print(0)
			exit()
		grid[c][i] = 'W'
ans = 0
for row in range(H):
	for col in range(W):
		if grid[row][col] is None:
			ans += 1
MOD = 10 ** 9 + 7
print(pow(2, ans, MOD))


sizes = [int(x) for x in input().split()]
h = sizes[0]
w = sizes[1]
n = [[2 for i in range(w)] for i in range(h)]
widths = [int(x) for x in input().split()]
partAns = 0
for i in range(h):
	partAns += widths[i]
	for j in range(widths[i]):
		n[i][j] = 1
	if widths[i] < w:
		partAns += 1
		n[i][widths[i]] = 0
pos = True
numCollision = 0
lengths = [int(x) for x in input().split()]
for i in range(w):
	partAns += lengths[i]
	for j in range(lengths[i]):
		if n[j][i] == 0:
			pos = False
			break
		if n[j][i] == 1:
			numCollision += 1
		else:
			n[j][i] = 1
	if lengths[i] < h:
		partAns += 1
		if n[lengths[i]][i] == 1:
			pos = False
			break
		if n[lengths[i]][i] == 0:
			numCollision += 1
		else:
			n[lengths[i]][i] = 0
	if pos == False:
		break
if pos == False:
	print(0)
else:
	print(pow(2, w * h - partAns + numCollision, 10 ** 9 + 7))


def printexit():
	print(0)
	exit(0)
(h, w) = list(map(int, input().strip().split()))
grid = [[0 for _ in range(w)] for _ in range(h)]
for (i, x) in enumerate(list(map(int, input().strip().split()))):
	for ii in range(x):
		if grid[i][ii] == 2:
			printexit()
		grid[i][ii] = 1
	if x < w and grid[i][x] == 1:
		printexit()
	if x < w:
		grid[i][x] = 2
for (i, x) in enumerate(list(map(int, input().strip().split()))):
	for ii in range(x):
		if grid[ii][i] == 2:
			printexit()
		grid[ii][i] = 1
	if x < h and grid[x][i] == 1:
		printexit()
	if x < h:
		grid[x][i] = 2
cnt = 0
for x in grid:
	for y in x:
		if y == 0:
			cnt += 1
print(pow(2, cnt, 10 ** 9 + 7))


(h, w) = [int(i) for i in input().strip().split(' ')]
r = [int(i) for i in input().strip().split(' ')]
c = [int(i) for i in input().strip().split(' ')]
matrix = [[0 for j in range(w)] for i in range(h)]
for i in range(h):
	for j in range(min(r[i], w)):
		matrix[i][j] = -1
for i in range(w):
	for j in range(min(c[i], h)):
		matrix[j][i] = -1
for i in range(h):
	if r[i] < w:
		if matrix[i][r[i]] == -1:
			print(0)
			exit(0)
for i in range(w):
	if c[i] < h:
		if matrix[c[i]][i] == -1:
			print(0)
			exit(0)
for i in range(h):
	if r[i] < w:
		matrix[i][r[i]] = -1
for i in range(w):
	if c[i] < h:
		matrix[c[i]][i] = -1
count = 0
for i in range(1, h):
	for j in range(1, w):
		if matrix[i][j] == 0:
			count += 1
print(2 ** count % 1000000007)


(a, b) = map(int, input().split())
h = map(int, input().split())
v = map(int, input().split())
room = [-1 for j in range(a * b)]
for (ind, i) in enumerate(h):
	for j in range(i):
		room[b * ind + j] = 1
	if i != b:
		room[b * ind + i] = 0
for (ind, i) in enumerate(v):
	for j in range(i):
		if room[b * j + ind] == 0:
			print(0)
			quit()
		room[b * j + ind] = 1
	if i != a:
		if room[b * i + ind] == 1:
			print(0)
			quit()
		room[b * i + ind] = 0
print(pow(2, room.count(-1), 1000000007))


(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)


MOD = 10 ** 9 + 7
(row, col) = map(int, input().split())
rdata = list(map(int, input().split()))
cdata = list(map(int, input().split()))
mat = [[-1] * col for _ in range(row)]
for i in range(row):
	temp = rdata[i]
	if temp < col:
		if mat[i][temp] == True:
			print(0)
			exit(0)
		mat[i][temp] = False
	elif temp > col:
		print(0)
		exit(0)
	for k in range(temp):
		if mat[i][k] == False:
			print(0)
			exit(0)
		mat[i][k] = True
for i in range(col):
	temp = cdata[i]
	if temp < row:
		if mat[temp][i] == True:
			print(0)
			exit(0)
		mat[temp][i] = False
	elif temp > row:
		print(0)
		exit(0)
	for k in range(temp):
		if mat[k][i] == False:
			print(0)
			exit(0)
		mat[k][i] = True
ans = 0
for i in range(row):
	for j in range(col):
		if mat[i][j] == -1:
			ans += 1
prod = 1
for i in range(ans):
	prod = prod * 2 % MOD
print(prod)


(r, c) = list(map(int, input().split()))
mat = [[False for _ in range(c)] for _ in range(r)]
rows = list(map(int, input().split()))
cols = list(map(int, input().split()))
for i in range(r):
	if rows[i] == 0:
		mat[i] = [True] + mat[i][1:]
	else:
		for j in range(rows[i]):
			mat[i][j] = True
		if j + 1 < c:
			mat[i][j + 1] = True
for i in range(c):
	if cols[i] == 0:
		mat[0][i] = True
	else:
		for j in range(cols[i]):
			mat[j][i] = True
		if j + 1 < r:
			mat[j + 1][i] = True
count = 0
for i in mat:
	count += i.count(False)
mod = 10 ** 9 + 7
f = cols[0]
t = rows[:f]
s = rows[0]
p = cols[:s]
for i in range(r):
	for j in range(c):
		if rows[i] - j == 0 and -cols[j] + i < 0:
			print(0)
			exit()
		if cols[j] - i == 0 and -rows[i] + j < 0:
			print(0)
			exit()
if 0 in t:
	print(0)
elif 0 in p:
	print(0)
else:
	print(pow(2, count, mod))


(h, w) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
Matrix = [[-1 for x in range(w)] for y in range(h)]

def f():
	for i in range(len(r)):
		for j in range(r[i]):
			if Matrix[i][j] == 0:
				return -1
			Matrix[i][j] = 1
		if r[i] == w:
			continue
		if Matrix[i][r[i]] == 1:
			return -1
		Matrix[i][r[i]] = 0
	for i in range(len(c)):
		for j in range(c[i]):
			if Matrix[j][i] == 0:
				return -1
			Matrix[j][i] = 1
		if c[i] == h:
			continue
		if Matrix[c[i]][i] == 1:
			return -1
		Matrix[c[i]][i] = 0
	return sum((row.count(-1) for row in Matrix))
try:
	re = f()
	if re == -1:
		print(0)
	else:
		print(2 ** f() % 1000000007)
except:
	print(0)


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()))
	cnt = h * w
	G = [[0] * w for _ in range(h)]
	checked = [[False] * w for _ in range(h)]
	for i in range(h):
		for j in range(R[i]):
			G[i][j] = 1
			checked[i][j] = True
			cnt -= 1
		if R[i] < w:
			checked[i][R[i]] = True
			cnt -= 1
	for j in range(w):
		for i in range(C[j]):
			if checked[i][j] and G[i][j] == 0:
				print(0)
				return
			G[i][j] = 1
			checked[i][j] = True
		if C[j] < h:
			if checked[C[j]][j] and G[C[j]][j] == 1:
				print(0)
				return
			checked[C[j]][j] = True
	cnt = 0
	for i in range(h):
		cnt += checked[i].count(False)
	print(2 ** cnt % 1000000007)
main()


def pm(mtr, m, n):
	for i in range(m):
		for j in range(n):
			print(mtr[i][j], end=' ')
		print('\n')
(m, n) = map(int, input().split())
rows = [int(x) for x in input().split()]
cols = [int(x) for x in input().split()]
mtr = [[0 for i in range(n)] for j in range(m)]
for i in range(m):
	for k in range(rows[i]):
		mtr[i][k] = 1
	if rows[i] <= n - 1:
		mtr[i][rows[i]] = 2
	if rows[i] == 0:
		mtr[i][0] = 2
b = False
for j in range(n):
	for k in range(cols[j]):
		if mtr[k][j] > 1:
			b = True
			break
		mtr[k][j] = 1
	if b:
		break
	if cols[j] == 0:
		if mtr[0][j] == 1:
			b = True
			break
		mtr[0][j] = 2
	if cols[j] <= m - 1:
		if mtr[cols[j]][j] == 1:
			b = True
			break
		else:
			mtr[cols[j]][j] = 2
cnt = 0
if not b:
	for i in range(m):
		for j in range(n):
			if mtr[i][j] == 0:
				cnt += 1
if b:
	print(0)
else:
	print(pow(2, cnt, 1000000007))


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)


(h, w) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
mat = [[0 for i in range(w)] for j in range(h)]
for i in range(h):
	k = min(r[i], w)
	for j in range(k):
		mat[i][j] = 1
	if r[i] < w:
		mat[i][r[i]] = 2
flag = 0
for i in range(w):
	k = min([c[i], h])
	for j in range(k):
		if mat[j][i] == 2:
			print(0)
			flag = 1
			break
		else:
			mat[j][i] = 1
	if flag == 1:
		break
	if c[i] < h:
		if mat[c[i]][i] == 1:
			print(0)
			break
		else:
			mat[c[i]][i] = 2
else:
	count = 0
	for i in range(h):
		for j in range(w):
			if mat[i][j] == 0:
				count += 1
	print(2 ** count % 1000000007)


def printf():
	for i in a:
		for j in i:
			print(j, end=' ')
		print()
(h, w) = map(int, input().split())
global a
a = [[0 for i in range(w)] for j in range(h)]
prolazi = True
r1 = list(map(int, input().split()))
for i in range(h):
	if not prolazi:
		break
	r = r1[i]
	j = 0
	if not r:
		if a[i][j] == 1:
			prolazi = False
		a[i][j] = -1
	else:
		while j < r and prolazi:
			if a[i][j] == -1 or a[i][j] == 2:
				prolazi = False
			a[i][j] = 1
			j += 1
		if r < w:
			if a[i][j] == 1:
				prolazi = False
			a[i][j] = 2
c1 = list(map(int, input().split()))
for i in range(w):
	if not prolazi:
		break
	c = c1[i]
	j = 0
	if not c:
		if a[j][i] == 1:
			prolazi = False
		a[j][i] = -1
	else:
		while j < c and prolazi:
			if a[j][i] == -1 or a[j][i] == 2:
				prolazi = False
			a[j][i] = 1
			j += 1
		if c < h:
			if a[j][i] == 1:
				prolazi = False
			a[j][i] = 2
sol = 0
if not prolazi:
	print(0)
else:
	for i in a:
		for j in i:
			if not j:
				sol += 1
	print(pow(2, sol, 1000000007))


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

def end():
	print(0)
	exit()
board = [x[:] for x in [[-1] * r] * c]
for i in range(c):
	for j in range(0, lx[i]):
		board[i][j] = 1
	if lx[i] < r:
		board[i][lx[i]] = 0
for i in range(r):
	for j in range(0, ly[i]):
		if board[j][i] == 0:
			end()
		board[j][i] = 1
	if ly[i] < c:
		if board[ly[i]][i] == 1:
			end()
		board[ly[i]][i] = 0
sum = 0
for i in range(c):
	for j in range(r):
		if board[i][j] == -1:
			sum += 1
print(pow(2, sum) % 1000000007)


import os

def f(r_arr, c_arr):
	result = 1
	for (i, r) in enumerate(r_arr):
		vars = 1
		for j in range(r):
			if c_arr[j] == i:
				return 0
		if r < len(c_arr) and c_arr[r] > i:
			return 0
		for j in range(r + 1, len(c_arr)):
			if c_arr[j] < i:
				vars *= 2
			else:
				vars *= 1
		result *= vars
		result %= 1000000007
	return result
if os.environ.get('DEBUG', False):
	print(f'{f([0, 3, 1], [0, 2, 3, 0])} = 2')
	print(f'{f([0], [1])} = 0')
	print(f'{f([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])} = 797922655')
else:
	list(map(int, input().split()))
	r_arr = list(map(int, input().split()))
	c_arr = list(map(int, input().split()))
	print(f(r_arr, c_arr))


def main():
	(h, w) = map(int, input().split())
	rs = list(map(int, input().split()))
	cs = list(map(int, input().split()))
	data = []
	for i in range(h):
		row = []
		for j in range(w):
			row.append([0, False])
		data.append(row)
	good_r = []
	for i in range(h):
		for j in range(rs[i]):
			if data[i][j] != [0, True]:
				data[i][j] = [1, True]
			else:
				return 0
		if rs[i] != w:
			if data[i][rs[i]] != [1, True]:
				data[i][rs[i]] = [0, True]
			else:
				return 0
			good_r.append(rs[i])
		else:
			good_r.append(-1)
	good_c = []
	for i in range(w):
		for j in range(cs[i]):
			if data[j][i] != [0, True]:
				data[j][i] = [1, True]
			else:
				return 0
		if cs[i] != h:
			if data[cs[i]][i] != [1, True]:
				data[cs[i]][i] = [0, True]
			else:
				return 0
			good_c.append(cs[i])
		else:
			good_c.append(-1)
	k = 1
	for i in range(h):
		for j in range(w):
			if not data[i][j][1]:
				if 0 <= good_r[i] and 0 <= good_c[j]:
					k *= 2
					k = k % 1000000007
	return k
print(main())


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import *
from copy import *
from collections import deque
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for pp in range(t):
		solve(pp)

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def google(p):
	print('Case #' + str(p) + ': ', end='')

def lcm(a, b):
	return a * b // gcd(a, b)

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def solve(case):
	(n, m) = sep()
	r = lis()
	c = lis()
	grid = [[-1 for i in range(m)] for j in range(n)]
	for i in range(n):
		for j in range(r[i]):
			if j >= m:
				print(0)
				return
			grid[i][j] = 1
		if r[i] < m:
			if grid[i][r[i]] == 1:
				print(0)
				return
			grid[i][r[i]] = 0
	for j in range(m):
		for i in range(c[j]):
			if i >= n:
				print(0)
				return
			if grid[i][j] == 0:
				print(0)
				return
			grid[i][j] = 1
		if c[j] < n:
			if grid[c[j]][j] == 1:
				print(0)
				return
			grid[c[j]][j] = 0
	cnt = 0
	for i in range(n):
		for j in range(m):
			if grid[i][j] == -1:
				cnt += 1
	ans = 1
	for i in range(cnt):
		ans *= 2
		ans %= mod
	print(ans % mod)
testcase(1)


from sys import stdin
input = stdin.readline
(h, w) = map(int, input().split())
r = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]

def count(h, w, r, x):
	t = [2] * h * w
	for (i, x) in enumerate(r):
		for j in range(x):
			t[w * i + j] = 1
			if j == w - 1:
				break
		else:
			t[w * i + x] = 0
	for (i, x) in enumerate(c):
		for j in range(x):
			if t[i + w * j] == 0:
				return 0
			t[i + w * j] = 1
			if j == h - 1:
				break
		else:
			if t[i + w * x] == 1:
				return 0
			t[i + w * x] = 0
	t = len(list(filter(lambda x: x == 2, t)))
	return 2 ** t % (10 ** 9 + 7)
print(count(h, w, r, c))


(rows, cols) = map(int, input().split())
a = [[-1] * cols for _ in range(rows)]
r = list(map(int, input().split()))
c = list(map(int, input().split()))
for row in range(rows):
	for col in range(r[row]):
		a[row][col] = 1
	if r[row] < cols:
		a[row][r[row]] = 0
for col in range(cols):
	for row in range(c[col]):
		a[row][col] = 1
	if c[col] < rows:
		a[c[col]][col] = 0
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) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
flag = 0
grid = {}
for i in range(h):
	for j in range(w):
		grid[i, j] = 0
for i in range(h):
	for j in range(r[i] + 1):
		if j == r[i]:
			grid[i, j] = -1
		elif j < w:
			grid[i, j] = 1
for i in range(w):
	for j in range(c[i] + 1):
		if j < h:
			if j != c[i] and grid[j, i] == -1:
				print(0)
				flag = 1
				break
			elif j == c[i] and grid[j, i] == 1:
				print(0)
				flag = 1
				break
			elif j == c[i]:
				grid[j, i] = -1
			else:
				grid[j, i] = 1
	if flag == 1:
		break
if flag == 0:
	count = list(grid.values()).count(0)
	print(2 ** count % (10 ** 9 + 7))


(h, w) = map(int, input().split(' '))
r = [int(k) for k in input().split(' ')]
c = [int(k) for k in input().split(' ')]
arr = [[-1 for i in range(w)] for j in range(h)]
possible = True
count = h * w
for i in range(h):
	if r[i] == 0:
		if arr[i][0] == '1':
			possible = False
			break
		if arr[i][0] == -1:
			count -= 1
			arr[i][0] = '0'
	else:
		for j in range(r[i]):
			if arr[i][j] == '0':
				possible = False
				break
			if arr[i][j] == -1:
				count -= 1
				arr[i][j] = '1'
		if j + 1 != w:
			if arr[i][j + 1] == '1':
				possible = False
				break
			if arr[i][j + 1] == -1:
				count -= 1
				arr[i][j + 1] = '0'
for j in range(w):
	if c[j] == 0:
		if arr[0][j] == '1':
			possible = False
			break
		if arr[0][j] == -1:
			count -= 1
			arr[0][j] = '0'
	else:
		for i in range(c[j]):
			if arr[i][j] == '0':
				possible = False
				break
			if arr[i][j] == -1:
				count -= 1
				arr[i][j] = '1'
		if i + 1 != h:
			if arr[i + 1][j] == '1':
				possible = False
				break
			if arr[i + 1][j] == -1:
				count -= 1
				arr[i + 1][j] = '0'
if not possible:
	print(0)
else:
	print(2 ** count % 1000000007)


mod = 1000000007
(h, w) = input().split()
h = int(h)
w = int(w)
rv = list(map(lambda x: int(x), input().split()))
cv = list(map(lambda x: int(x), input().split()))
x = [[-1 for i in range(w)] for j in range(h)]
f = True
for i in range(h):
	l = rv[i]
	for j in range(l):
		x[i][j] = 1
	if l != w:
		x[i][l] = 0
for j in range(w):
	l = cv[j]
	for i in range(l):
		if x[i][j] != -1 and x[i][j] == 0:
			f = False
		x[i][j] = 1
	if l != h:
		if x[l][j] != -1 and x[l][j] == 1:
			f = False
		x[l][j] = 0
if f:
	c = 0
	for i in range(h):
		for j in range(w):
			if x[i][j] == -1:
				c += 1
	print(2 ** c % mod)
else:
	print(0)


(h, w) = map(int, input().split())
r = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]
a = [[str('N') for i in range(w)] for j in range(h)]
flag = 0
for i in range(h):
	t = r[i]
	for j in range(t):
		a[i][j] = 'T'
	if t + 1 <= w:
		a[i][t] = 'F'
for i in range(w):
	t = c[i]
	for j in range(t):
		if a[j][i] != 'F':
			a[j][i] = 'T'
		else:
			flag = 1
			break
	if flag == 1:
		break
	if t + 1 <= h:
		if a[t][i] != 'T':
			a[t][i] = 'F'
		else:
			flag = 1
			break
	if flag == 1:
		break
ans = 0
for i in range(h):
	ans += a[i].count('N')
if flag == 1:
	output = 0
else:
	output = 2 ** ans % (10 ** 9 + 7)
print(output)


import array
import math
import os
import sys
DEBUG = 'DEBUG' in os.environ

def inp():
	return sys.stdin.readline().rstrip()

def dprint(*value, sep=' ', end='\n'):
	if DEBUG:
		print(*value, sep=sep, end=end)
MOD = 10 ** 9 + 7

def solve(H, W, R, C):
	BLACK = 1
	WHITE = 2
	F = [array.array('i', [0] * W) for _ in range(H)]
	for y in range(H):
		r = R[y]
		for x in range(r):
			F[y][x] = BLACK
		if r < W:
			F[y][r] = WHITE
	for x in range(W):
		c = C[x]
		for y in range(c):
			if F[y][x] == WHITE:
				return 0
			F[y][x] = BLACK
		if c < H:
			if F[c][x] == BLACK:
				return 0
			F[c][x] = WHITE
	ans = 1
	for y in range(H):
		for x in range(W):
			if F[y][x] == 0:
				ans *= 2
				ans %= MOD
	return ans

def main():
	(H, W) = [int(e) for e in inp().split()]
	R = [int(e) for e in inp().split()]
	C = [int(e) for e in inp().split()]
	print(solve(H, W, R, C))
main()


def un(h, w, r, c):
	unknown = h * w
	for i in range(0, h):
		unknown -= r[i]
		if r[i] < w:
			unknown -= 1
	for i in range(0, w):
		unknown -= c[i]
		if c[i] < h:
			unknown -= 1
	for i in range(0, h):
		for j in range(0, r[i]):
			if c[j] >= i + 1:
				unknown += 1
			if c[j] == i and j < r[i]:
				return 0
		if r[i] < w and c[r[i]] == i:
			unknown += 1
		if r[i] < w and c[r[i]] >= i + 1:
			return 0
	return 2 ** unknown % 1000000007
[h, w] = [int(x) for x in input().split()]
r = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]
print(un(h, w, r, c))


[h, w] = [int(k) for k in input().split()]
R = [int(k) for k in input().split()]
C = [int(k) for k in input().split()]
c = 0
flag = 0
for i in range(h):
	for j in range(w):
		if i > C[j] and j > R[i]:
			c = c + 1
		if i == C[j] and j < R[i]:
			print(0)
			flag = 1
			break
		if i < C[j] and j == R[i]:
			print(0)
			flag = 1
			break
	if flag == 1:
		break
if flag == 0:
	if c == 0:
		print(1)
	else:
		ans = 2 ** c % 1000000007
		print(ans)


(h, w) = map(int, input().split())
(r, c) = ([0] * h, [0] * w)
r = list(map(int, input().split()))
c = list(map(int, input().split()))
board = [[-1 for i in range(w)] for j in range(h)]
for (ct, x) in enumerate(r):
	if x == 0:
		board[ct][0] = 0
	else:
		for i in range(x):
			board[ct][i] = 1
		if x < w:
			board[ct][x] = 0
for (ct, x) in enumerate(c):
	if x == 0:
		board[0][ct] = 0
	else:
		for i in range(x):
			board[i][ct] = 1
		if x < h:
			board[x][ct] = 0
er = False
for (ct, x) in enumerate(r):
	if x == 0:
		if board[ct][0] != 0:
			er = True
	else:
		for i in range(x):
			if board[ct][i] != 1:
				er = True
		if x < w:
			if board[ct][x] != 0:
				er = True
count = 0
for x in board:
	count += x.count(-1)
if er == True:
	print(0)
else:
	print(pow(2, count, 10 ** 9 + 7))


import sys
import math
from collections import defaultdict, deque
import heapq
try:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
except:
	pass

def takeInput():
	return [int(x) for x in input().strip().split()]
(r, c) = takeInput()
rows = takeInput()
cols = takeInput()
filled = [[False for i in range(c + 2)] for j in range(r + 2)]
idx = 1
for i in rows:
	for j in range(1, i + 2):
		filled[idx][j] = True
	idx += 1
idx = 1
for i in cols:
	for j in range(1, i + 2):
		filled[j][idx] = True
	idx += 1
possible = True
free = 0
for i in range(1, r + 1):
	for j in range(1, c + 1):
		if not filled[i][j]:
			free += 1
filled = [[False for i in range(c + 1)] for j in range(r + 1)]
idx = 1
for i in rows:
	for j in range(1, i + 1):
		key = str(idx) + ':' + str(j)
		filled[idx][j] = True
	idx += 1
idx = 1
for i in cols:
	for j in range(1, i + 1):
		filled[j][idx] = True
	idx += 1
idx = 1
for i in rows:
	j = 1
	current_count = 0
	while j <= c and filled[idx][j]:
		current_count += 1
		j += 1
	if current_count != i:
		possible = False
		break
	idx += 1
idx = 1
for i in cols:
	j = 1
	current_count = 0
	while j <= r and filled[j][idx]:
		current_count += 1
		j += 1
	if current_count != i:
		possible = False
		break
	idx += 1
if not possible:
	print(0)
else:
	print(pow(2, free, 10 ** 9 + 7))


(h, w) = map(int, input().split())
(*R,) = map(int, input().split())
(*C,) = map(int, input().split())
MOD = 10 ** 9 + 7
MP = [[-1] * w for i in range(h)]
for i in range(h):
	MPi = MP[i]
	for j in range(R[i]):
		MPi[j] = 1
	if R[i] < w:
		MPi[R[i]] = 0
ok = 1
for j in range(w):
	for i in range(C[j]):
		if MP[i][j] == 0:
			ok = 0
		MP[i][j] = 1
	if C[j] < h:
		if MP[C[j]][j] == 1:
			ok = 0
		MP[C[j]][j] = 0
cnt = 0
for i in range(h):
	MPi = MP[i]
	for j in range(w):
		if MPi[j] == -1:
			cnt += 1
print(pow(2, cnt, MOD) if ok else 0)


(a, b) = map(int, input().split())
if a == 0 or b == 0:
	print(1)
else:
	r = list(map(int, input().split()))
	c = list(map(int, input().split()))
	tabuleiro = [[-1 for _ in range(b)] for _ in range(a)]
	for i in range(a):
		for j in range(r[i]):
			tabuleiro[i][j] = 1
	for j in range(b):
		for i in range(c[j]):
			tabuleiro[i][j] = 1
	for i in range(a):
		contador = 0
		for j in range(b):
			if tabuleiro[i][j] == 1:
				contador += 1
			else:
				break
		if r[i] != contador:
			print(0)
			exit()
	for j in range(b):
		contador = 0
		for i in range(a):
			if tabuleiro[i][j] == 1:
				contador += 1
			else:
				break
		if c[j] != contador:
			print(0)
			exit()
	contador = 0
	for i in range(1, a):
		for j in range(1, b):
			if tabuleiro[i][j] == -1 and r[i] != j and (c[j] != i):
				contador += 1
	mod = int(1000000000.0 + 7)
	ans = 1
	for i in range(contador):
		ans <<= 1
		ans %= mod
	print(ans)


def validaValores(a1, a2):
	for i in range(len(a1)):
		valor = a1[i]
		if valor < len(a2):
			vcol = a2[valor]
			if i + 1 <= vcol and vcol >= valor:
				return False
	for j in range(len(a2)):
		valor = a2[j]
		if valor < len(a1):
			vLin = a1[valor]
			if j + 1 <= vLin and vLin >= valor:
				return False
	return True
(numRows, numCols) = [*map(int, input().split())]
valueLinhas = [*map(int, input().split())]
valueCols = [*map(int, input().split())]
modulo = 1000000007
total = numRows * numCols
disponiveis = 0
invalido = not validaValores(valueLinhas, valueCols)
if invalido:
	resp = 0
else:
	for i in range(numRows):
		for j in range(numCols):
			vl = valueLinhas[i]
			vc = valueCols[j]
			if not ((vl == 0 or vc == 0) and (i == 0 or j == 0)) and (not vl >= j) and (not vc >= i):
				disponiveis += 1
	resp = pow(2, disponiveis) % modulo
print(resp)


(h, w) = [int(i) for i in input().split()]
r = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]
b = [-1] * w
a = []
for i in range(h):
	a.append(b[:])
for i in range(h):
	for j in range(r[i]):
		a[i][j] = 1
	if r[i] < w:
		a[i][r[i]] = 0
fl = True
for i in range(w):
	for j in range(c[i]):
		if a[j][i] == 0:
			fl = False
			break
		a[j][i] = 1
	if not fl:
		break
	if c[i] < h:
		if a[c[i]][i] == 1:
			fl = False
			break
		a[c[i]][i] = 0
if not fl:
	print(0)
else:
	ans = 1
	for i in range(h):
		for j in range(w):
			if a[i][j] == -1:
				ans *= 2
				ans %= 1000000007
	print(ans)


(h, w) = [int(s) for s in input().split()]
pos = 1
sq1 = [0] * (h + 1)
for i in range(h + 1):
	sq1[i] = [-1] * (w + 1)
sq2 = [0] * (h + 1)
for i in range(h + 1):
	sq2[i] = [-1] * (w + 1)
arr = [int(s) for s in input().split()]
for i in range(h):
	for j in range(arr[i]):
		sq1[i][j] = 1
	sq1[i][arr[i]] = 0
arr = [int(s) for s in input().split()]
for i in range(w):
	j = 0
	for j in range(arr[i]):
		sq2[j][i] = 1
	sq2[arr[i]][i] = 0
ans = 0
for i in range(h):
	for j in range(w):
		if sq1[i][j] + sq2[i][j] == 1:
			pos = 0
		if sq1[i][j] + sq2[i][j] == -2:
			ans += 1
print(2 ** ans * pos % 1000000007)


(n, m) = input().split()
n = int(n)
m = int(m)
arr1 = list(map(int, input().split()))
arr2 = list(map(int, input().split()))
count = 0
k = False
for i in range(n):
	for j in range(m):
		if arr1[i] < j and arr2[j] < i:
			count += 1
		if arr1[i] == j and arr2[j] > i or (arr2[j] == i and arr1[i] > j):
			k = True
			break
if k == 1:
	print(0)
else:
	print(pow(2, count, 1000000007))


(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)


(h, w) = [int(x) for x in input().split()]
r = list(map(int, input().split()))
c = list(map(int, input().split()))
a = []
for i in range(h):
	t = [-1] * w
	a.append(t)
count = 0
ch = 0
for i in range(len(r)):
	for j in range(r[i]):
		if j >= w:
			break
		if a[i][j] != 0:
			a[i][j] = 1
		else:
			ch = 1
			break
	if r[i] < w:
		if a[i][r[i]] == 1:
			ch = 1
		a[i][r[i]] = 0
	if ch:
		break
for i in range(len(c)):
	for j in range(c[i]):
		if j >= h:
			break
		if a[j][i] != 0:
			a[j][i] = 1
		else:
			ch = 1
			break
	if c[i] < h:
		if a[c[i]][i] == 1:
			ch = 1
		a[c[i]][i] = 0
	if ch:
		break
if ch:
	print(0)
else:
	for i in range(h):
		for j in range(w):
			if a[i][j] == -1:
				count -= -1
	else:
		print(2 ** count % 1000000007)


(h, w) = [int(x) for x in input().strip().split(' ')]
r = [int(x) for x in input().strip().split(' ')]
c = [int(x) for x in input().strip().split(' ')]
arr = [[0 for _ in range(w)] for _ in range(h)]
for (i, e) in enumerate(r):
	for j in range(e):
		if arr[i][j] == -1:
			print(0)
			exit()
		arr[i][j] = 1
	if e < w:
		arr[i][e] = -1
for (j, e) in enumerate(c):
	for i in range(e):
		if arr[i][j] == -1:
			print(0)
			exit()
		arr[i][j] = 1
	if e < h:
		arr[e][j] = -1
arr = [[0 for _ in range(w)] for _ in range(h)]
for (j, e) in enumerate(c):
	for i in range(e):
		if arr[i][j] == -1:
			print(0)
			exit()
		arr[i][j] = 1
	if e < h:
		arr[e][j] = -1
for (i, e) in enumerate(r):
	for j in range(e):
		if arr[i][j] == -1:
			print(0)
			exit()
		arr[i][j] = 1
	if e < w:
		arr[i][e] = -1
cnt = 0
for e in arr:
	cnt += e.count(0)
print(pow(2, cnt, 1000000007))


(h, w) = list(map(int, input().split()))
arrR = list(map(int, input().split()))
arrC = list(map(int, input().split()))
arr = [[0 for y in range(w)] for i in range(h)]
result = 0
ok = True
for i in range(h):
	for y in range(w):
		if arrR[i] == y and arrC[y] > i or (arrR[i] > y and arrC[y] == i):
			ok = False
			break
		if y == 0 and arrR[i] == 0 or (i == 0 and arrC[y] == 0):
			arr[i][y] = 2
			continue
		if arrR[i] > y:
			arr[i][y] = 1
			if y + 1 <= w - 1 and y + 1 == arrR[i]:
				arr[i][y + 1] = 2
		if arrC[y] > i:
			arr[i][y] = 1
			if i + 1 <= h - 1 and i + 1 == arrC[y]:
				arr[i + 1][y] = 2
for el in arr:
	result += el.count(0)
if ok:
	print(pow(2, result) % 1000000007)
else:
	print(0)


def main():
	(h, w) = [int(x) for x in input().split()]
	r = [int(x) for x in input().split()]
	c = [int(x) for x in input().split()]
	ans = 1
	grid = [[-1 for _ in range(w)] for __ in range(h)]
	for (row, cnts) in enumerate(r):
		idx = 0
		for _ in range(cnts):
			grid[row][idx] = 1
			idx += 1
		if idx < w:
			grid[row][idx] = 0
	for (col, cnts) in enumerate(c):
		idx = 0
		for _ in range(cnts):
			if grid[idx][col] == 0:
				ans = 0
				break
			else:
				grid[idx][col] = 1
				idx += 1
		if idx < h:
			if grid[idx][col] == 1:
				ans = 0
			else:
				grid[idx][col] = 0
	MOD = 10 ** 9 + 7
	for i in range(h):
		for j in range(w):
			if grid[i][j] == -1:
				ans *= 2
				ans %= MOD
	print(ans)
	return
import sys
input = sys.stdin.buffer.readline

def oneLineArrayPrint(arr):
	print(' '.join([str(x) for x in arr]))

def multiLineArrayPrint(arr):
	print('\n'.join([str(x) for x in arr]))

def multiLineArrayOfArraysPrint(arr):
	print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))
main()


import os
import sys
from math import *
from collections import *
from bisect import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]

def nCr(n, r):
	p = 1
	k = 1
	if n - r < r:
		r = n - r
	if r != 0:
		while r:
			p *= n
			k *= r
			m = gcd(p, k)
			p //= m
			k //= m
			n -= 1
			r -= 1
	else:
		p = 1
	return p

def valid(c, r, row, col):
	m = [[1 for i in range(r)] for i in range(c)]
	for i in range(c):
		for j in range(min(row[i] + 1, r)):
			m[i][j] = 0
		if row[i] < r and col[row[i]] >= i + 1:
			return False
	for j in range(r):
		for i in range(min(col[j] + 1, c)):
			m[i][j] = 0
		if col[j] < c and row[col[j]] >= j + 1:
			return False
	return True
m = 1000000007
(r, c) = value()
row = array()
col = array()
ans = 0
if not valid(r, c, row, col):
	print(0)
	exit()
for i in range(r):
	for j in range(row[i] + 1, c):
		if col[j] <= i - 1:
			ans += 1
print(2 ** ans % m)


(h, w) = [int(i) for i in input().split(' ')]
r = [int(i) for i in input().split(' ')]
c = [int(i) for i in input().split(' ')]
grid = [[-1 for i in range(w)] for j in range(h)]
flag = True
for i in range(h):
	for j in range(r[i]):
		grid[i][j] = 1
	if r[i] < w:
		grid[i][r[i]] = 0
for i in range(w):
	for j in range(c[i]):
		if grid[j][i] == 0:
			flag = False
			break
		grid[j][i] = 1
	if flag and c[i] < h:
		if grid[c[i]][i] == 1:
			flag = False
		else:
			grid[c[i]][i] = 0
	if not flag:
		break
if not flag:
	print(0)
else:
	cnt = 0
	for i in range(h):
		for j in range(w):
			cnt += grid[i][j] == -1
	print(2 ** cnt % 1000000007)


from sys import stdin, stdout
(n, m) = [int(x) for x in stdin.readline().split()]
r = [int(x) for x in stdin.readline().split()]
c = [int(x) for x in stdin.readline().split()]
ok = True
cnt = 0
for i in range(0, n):
	for j in range(0, m):
		if i == c[j] and j < r[i]:
			ok = False
			break
		if j == r[i] and i < c[j]:
			ok = False
			break
		if i > c[j] and j > r[i]:
			cnt += 1
	if not ok:
		break
if not ok:
	stdout.write(str(0))
else:
	ans = pow(2, cnt, 10 ** 9 + 7)
	stdout.write(str(ans))


(h, w) = list(map(int, input().split()))
r = list(map(int, input().split()))
c = list(map(int, input().split()))
m = [[0 for col in range(w)] for row in range(h)]
for i in range(h):
	if r[i] == 0:
		m[i][0] = 1
	else:
		for j in range(r[i]):
			m[i][j] = -1
		if r[i] < w:
			m[i][r[i]] = 1
zero = False
for j in range(w):
	if c[j] == 0:
		if m[0][j] == 0 or m[0][j] == 1:
			m[0][j] = 1
		else:
			zero = True
	else:
		for i in range(c[j]):
			if m[i][j] == 0 or m[i][j] == -1:
				m[i][j] = -1
			else:
				zero = True
		if c[j] < h:
			if m[c[j]][j] == 0 or m[c[j]][j] == 1:
				m[c[j]][j] = 1
			else:
				zero = True
if zero:
	print(0)
else:
	k = 0
	for i in range(h):
		for j in range(w):
			if m[i][j] == 0:
				k += 1
	print(2 ** k % 1000000007)


import sys
import math as mt
input = sys.stdin.buffer.readline
t = 1
mod = 10 ** 9 + 7
for __ in range(t):
	(n, m) = map(int, input().split())
	a = [[0 for j in range(m)] for i in range(n)]
	r = list(map(int, input().split()))
	c = list(map(int, input().split()))
	for i in range(n):
		for j in range(r[i]):
			a[i][j] = 1
	for j in range(m):
		for i in range(c[j]):
			a[i][j] = 1
	ch = 0
	for i in range(n):
		cnt = 0
		for j in range(m):
			if a[i][j] == 1:
				cnt += 1
			else:
				break
		if r[i] != cnt:
			ch = 1
	for j in range(m):
		cnt = 0
		for i in range(n):
			if a[i][j] == 1:
				cnt += 1
			else:
				break
		if cnt != c[j]:
			ch = 1
	if ch == 1:
		print(0)
	else:
		ans = 0
		fans = 2 ** 0
		for i in range(1, n):
			for j in range(r[i] + 1, m):
				if c[j] < i:
					fans = 2 * (fans % mod) % mod
		print(fans % mod)


from sys import stdin, stdout
input = stdin.readline

def main():
	t = 1
	for i in range(t):
		(n, m) = map(int, input().split())
		ai = list(map(int, input().split()))
		bi = list(map(int, input().split()))
		mod = 10 ** 9 + 7
		table = [[0] * m for i in range(n)]
		for i in range(n):
			for j in range(ai[i]):
				table[i][j] = 1
			j = ai[i]
			if j < m:
				table[i][j] = 2
		for i in range(m):
			for j in range(bi[i]):
				if table[j][i] == 2:
					print(0)
					return
				table[j][i] = 1
			j = bi[i]
			if j < n:
				if table[j][i] == 1:
					print(0)
					return
				table[j][i] = 2
		ans = 0
		for i in range(n):
			for j in range(m):
				ans += int(table[i][j] == 0)
		print(pow(2, ans, mod))
main()


(h, w) = map(int, input().split())
r = input().split()
c = input().split()
for i in range(h):
	r[i] = int(r[i])
for i in range(w):
	c[i] = int(c[i])
a = [[-1 for i in range(w)] for j in range(h)]
for i in range(h):
	j = 0
	while j < r[i]:
		a[i][j] = 1
		j += 1
	if j < w:
		a[i][j] = 0
flag = True
for j in range(w):
	i = 0
	while i < c[j]:
		if a[i][j] == 0:
			flag = False
		a[i][j] = 1
		i += 1
	if i < h:
		if a[i][j] == 1:
			flag = False
		a[i][j] = 0
count = 0
for i in range(h):
	for j in range(w):
		if a[i][j] == -1:
			count += 1
if not flag:
	print(0)
elif count == 0:
	print(1)
else:
	ans = 1
	for i in range(count):
		ans *= 2
		ans %= 1000000007
	print(ans)


(n, m) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
t = True
free = [[1 for i in range(m)] for i in range(n)]
for i in range(n):
	for j in range(min(r[i] + 1, m)):
		free[i][j] = 0
	if r[i] < m and c[r[i]] >= i + 1:
		t = False
for j in range(m):
	for i in range(min(c[j] + 1, n)):
		free[i][j] = 0
	if c[j] < n and r[c[j]] >= j + 1:
		t = False
ans = 0
for i in range(n):
	for j in range(m):
		if free[i][j]:
			ans += 1
if t:
	print(2 ** ans % (10 ** 9 + 7))
else:
	print(0)


import sys
input = sys.stdin.readline

def exp(n):
	if n == 0:
		return 1
	if n == 1:
		return 2
	elif n % 2 == 0:
		tmp = exp(n // 2) % MOD
		return tmp * tmp % MOD
	else:
		tmp = exp(n // 2) % MOD
		return tmp * tmp % MOD * 2 % MOD

def main():
	for i in range(h):
		if a[i] == 0:
			board[i][0] = 0
		else:
			for j in range(a[i]):
				board[i][j] = 1
			board[i][j + 1] = 0
	for j in range(w):
		if b[j] == 0:
			if board[0][j] == 1:
				print(0)
				return
			else:
				board[0][j] = 0
		else:
			for i in range(b[j]):
				if board[i][j] == 0:
					print(0)
					return
				else:
					board[i][j] = 1
			if board[i + 1][j] == 1:
				print(0)
				return
			else:
				board[i + 1][j] = 0
	cnt = 0
	for r in range(h):
		for c in range(w):
			if board[r][c] == -1:
				cnt += 1
	print(exp(cnt))
	return
MOD = 1000000007
(h, w) = map(int, input().split())
board = [[-1 for j in range(w + 1)] for i in range(h + 1)]
a = list(map(int, input().split()))
b = list(map(int, input().split()))
main()


(h, w) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
ans = 1
flag = False
for i in range(h):
	for j in range(w):
		if i == c[j] and j < r[i] or (i < c[j] and j == r[i]):
			print(0)
			flag = True
			break
		if not (i <= c[j] or j <= r[i]):
			ans *= 2
			ans %= 10 ** 9 + 7
	if flag:
		break
else:
	print(ans)


(h, w) = map(int, input().split())
ar = list(map(int, input().split()))
br = list(map(int, input().split()))
flag = True
count = 0
for i in range(h):
	for j in range(w):
		c_1 = ''
		c_2 = ''
		if ar[i] > j:
			c_1 = 'B'
		elif ar[i] < j:
			c_1 = 'E'
		elif ar[i] == j:
			c_1 = 'W'
		if br[j] > i:
			c_2 = 'B'
		elif br[j] < i:
			c_2 = 'E'
		elif br[j] == i:
			c_2 = 'W'
		if c_1 == c_2 and c_1 == 'E':
			count += 1
		elif c_1 != c_2 and c_1 != 'E' and (c_2 != 'E'):
			flag = False
			break
if flag:
	print(2 ** count % (10 ** 9 + 7))
else:
	print(0)


def fastio():
	import sys
	from io import StringIO
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip('\r\n')
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()
MOD = 10 ** 9 + 7
I = lambda : list(map(int, input().split()))
(h, w) = I()
r = I()
c = I()
m = [[-1] * (w + 1) for i in range(h + 1)]
for i in range(h):
	k = r[i]
	for j in range(k):
		if m[i][j] == 1:
			print(0)
			exit()
		m[i][j] = 0
	if k < w:
		if m[i][k] == 0:
			print(0)
			exit()
		m[i][k] = 1
for i in range(w):
	k = c[i]
	for j in range(k):
		if m[j][i] == 1:
			print(0)
			exit()
		m[j][i] = 0
	if k < h:
		if m[k][i] == 0:
			print(0)
			exit()
		m[k][i] = 1
ans = 0
for i in range(h):
	for j in range(w):
		u = r[i]
		v = c[j]
		if m[i][j] == -1:
			ans += 1
print(pow(2, ans, MOD))


(h, w) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
b = [[0 for _ in range(w)] for _ in range(h)]
count = 0
ans = 1
for i in range(h):
	if r[i] == 0:
		b[i][0] -= 1
	else:
		for k in range(r[i]):
			b[i][k] += 2
		if w > r[i]:
			b[i][r[i]] -= 1
for j in range(w):
	if c[j] == 0:
		b[0][j] -= 1
	else:
		for l in range(c[j]):
			b[l][j] += 2
		if h > c[j]:
			b[c[j]][j] -= 1
for p in range(h):
	for q in range(w):
		if b[p][q] == 1:
			print('0')
			exit()
		elif b[p][q] == 0:
			count += 1
for o in range(count):
	ans = ans * 2 % (10 ** 9 + 7)
print(ans)


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)
			sys.exit()
		MAP[y][x] = 1
	if r < W:
		if MAP[y][r] == 1:
			print(0)
			sys.exit()
		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))


def main():
	mod = 1000000007
	(h, w) = map(int, input().split())
	r = [int(c) for c in input().split()]
	c = [int(c) for c in input().split()]
	grid = [[-1] * w for _ in range(h)]
	for (i, row) in enumerate(r):
		for j in range(row):
			grid[i][j] = 1
		if row < w:
			grid[i][row] = 0
	for (j, col) in enumerate(c):
		for i in range(col):
			if grid[i][j] == 0:
				print(0)
				return
			else:
				grid[i][j] = 1
		if col < h:
			if grid[col][j] == 1:
				print(0)
				return
			else:
				grid[col][j] = 0
	choices = 0
	for i in range(h):
		for j in range(w):
			if grid[i][j] == -1:
				choices += 1
	print(pow(2, choices, mod))
main()


def func(h, w, r, c):
	fill = 0
	for i in range(h):
		for j in range(w):
			if i > c[j] and j > r[i]:
				fill += 1
			if j == r[i] and i < c[j] or (i == c[j] and j < r[i]):
				return 0
	return 2 ** fill % 1000000007
(h, w) = tuple(map(int, input().split(' ')))
r = list(map(int, input().split(' ')))
c = list(map(int, input().split(' ')))
print(func(h, w, r, c))


MOD = 1000000007
(h, w) = [int(i) for i in input().split()]
r = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]
mat = [[0 for i in range(w)] for j in range(h)]
flag = 0
for i in range(h):
	for j in range(w):
		if r[i] > 0 and c[j] > 0:
			r[i] -= 1
			c[j] -= 1
			mat[i][j] = 3
		elif r[i] > 0 or c[j] > 0:
			if r[i] > 0 and (i != 0 and (mat[i - 1][j] == 0 or mat[i - 1][j] == 1)):
				r[i] -= 1
				mat[i][j] = 1
			elif c[j] > 0 and (j != 0 and (mat[i][j - 1] == 0 or mat[i][j - 1] == 2)):
				c[j] -= 1
				mat[i][j] = 2
			else:
				flag = 1
count = 1
for i in range(1, h):
	for j in range(1, w):
		if mat[i][j] == 0:
			if mat[i][j - 1] != 3 and mat[i][j - 1] != 1:
				if mat[i - 1][j] != 3 and mat[i - 1][j] != 2:
					count = count * 2 % MOD
if flag == 0:
	print(count % MOD)
else:
	print(0)


(h, w) = map(int, input().split())
grid = [[-1 for i in range(w)] for j in range(h)]
rl = list(map(int, input().split()))
cl = list(map(int, input().split()))
for i in range(len(rl)):
	for j in range(rl[i]):
		grid[i][j] = 1
	if rl[i] < w:
		grid[i][rl[i]] = 0
for i in range(len(cl)):
	for j in range(cl[i]):
		if grid[j][i] == 0:
			print(0)
			exit(0)
		else:
			grid[j][i] = 1
	if cl[i] < h:
		if grid[cl[i]][i] == 1:
			print(0)
			exit(0)
		grid[cl[i]][i] = 0
count = 0
for i in grid:
	for j in i:
		if j == -1:
			count += 1
print(pow(2, count, 10 ** 9 + 7))


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()


def quickpow(a, b, m):
	ans = 1
	while b > 0:
		if b & 1:
			ans = ans * a % m
		a = a * a % m
		b = b >> 1
	return ans

def main():
	while True:
		try:
			(n, m) = map(int, input().strip().split())
			h = list(map(int, input().strip().split()))
			w = list(map(int, input().strip().split()))
			vis = [-1] * (m + 2)
			vis = [vis.copy() for i in range(n + 2)]
			k = 0
			ok = True
			for i in range(n):
				for j in range(h[i]):
					vis[i][j] = 1
				vis[i][h[i]] = 0
			for j in range(m):
				for i in range(w[j]):
					if vis[i][j] == 0:
						ok = False
					vis[i][j] = 1
				if vis[w[j]][j] == 1:
					ok = False
				vis[w[j]][j] = 0
			if ok == False:
				print('0')
				continue
			for i in range(n):
				for j in range(m):
					k += 1 if vis[i][j] == -1 else 0
			print(int(quickpow(2, k, int(1000000000.0 + 7))))
		except EOFError:
			break
main()


(h, w) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
mod = 1000000007
grid = [['?' for i in range(w + 1)] for j in range(h + 1)]

def try_set(row, col, target):
	if grid[row][col] == '?':
		grid[row][col] = target
	elif grid[row][col] != target:
		raise ValueError

def go():
	try:
		for row in range(h):
			for col in range(r[row]):
				try_set(row, col, 'FULL')
			try_set(row, r[row], 'EMPTY')
		for col in range(w):
			for row in range(c[col]):
				try_set(row, col, 'FULL')
			try_set(c[col], col, 'EMPTY')
	except ValueError:
		return 0
	answer = 1
	for i in range(h):
		for j in range(w):
			if grid[i][j] == '?':
				answer = answer * 2 % mod
	return answer
print(go())


from itertools import accumulate, chain, combinations, groupby, permutations, product
from collections import deque, Counter
from bisect import bisect_left, bisect_right
from math import gcd, sqrt, sin, cos, tan, degrees, radians
from fractions import Fraction
from decimal import Decimal
import sys
MOD = 10 ** 9 + 7
INF = 10 ** 20
(h, w) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
ans = [[-1] * 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]] = 0
for j in range(w):
	for i in range(c[j]):
		if ans[i][j] != 0:
			ans[i][j] = 1
		else:
			print(0)
			exit()
	if c[j] != h:
		if ans[c[j]][j] != 1:
			ans[c[j]][j] = 0
		else:
			print(0)
			exit()
s = 0
for i in range(h):
	for j in range(w):
		s += ans[i][j] == -1
print(pow(2, s, MOD))


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
(h, w) = map(int, input().split())
rows = list(map(int, input().split()))
col = list(map(int, input().split()))
nums = 0
broke = False
for s in range(h):
	for b in range(w):
		if b >= rows[s] + 1 and s >= col[b] + 1:
			nums += 1
		elif rows[s] == b and col[b] >= s + 1 or (col[b] == s and rows[s] >= b + 1):
			broke = True
			break
if broke == False:
	mod = 10 ** 9 + 7
	print(pow(2, nums, mod))
else:
	print(0)


(h, w) = map(int, input().split())
rs = [int(x) for x in input().split()]
cs = [int(x) for x in input().split()]

def boardOptions(h, w, rs, cs):
	filled = 0
	board = {}
	for (i, r) in enumerate(rs):
		for j in range(r):
			if (i, j) not in board:
				board[i, j] = 1
				filled += 1
			elif board[i, j] == 0:
				return 0
		if r < w:
			if (i, r) not in board:
				board[i, r] = 0
				filled += 1
			elif board[i, r] == 1:
				return 0
	for (j, c) in enumerate(cs):
		for i in range(c):
			if (i, j) not in board:
				board[i, j] = 1
				filled += 1
			elif board[i, j] == 0:
				return 0
		if c < h:
			if (c, j) not in board:
				board[c, j] = 0
				filled += 1
			elif board[c, j] == 1:
				return 0
	undefined = h * w - filled
	return 2 ** undefined % 1000000007
print(boardOptions(h, w, rs, cs))


(h, w) = list(map(int, input().strip().split()))
r = list(map(int, input().strip().split()))
c = list(map(int, input().strip().split()))
s = 0
flag = True
for i in range(h):
	for j in range(w):
		if j > r[i] and i > c[j]:
			s += 1
		elif c[j] >= i + 1 and r[i] == j or (c[j] == i and r[i] >= j + 1):
			print('0')
			flag = False
			break
	if flag == False:
		break
if flag:
	print(2 ** s % 1000000007)


hw = input().split()
h = int(hw[0])
w = int(hw[1])
f = 0
l = [[2] * w for i in range(h)]
r = list(map(int, input().split()))
for i in range(0, h):
	if r[i] == 0:
		l[i][0] = 0
	else:
		for j in range(0, r[i]):
			l[i][j] = 1
		if r[i] < w:
			l[i][j + 1] = 0
c = list(map(int, input().split()))
for i in range(0, w):
	if c[i] == 0:
		if l[0][i] == 1:
			f = 1
			break
		else:
			l[0][i] = 0
	else:
		for j in range(0, c[i]):
			if l[j][i] == 0:
				f = 1
				break
			else:
				l[j][i] = 1
		if c[i] < h:
			if l[j + 1][i] == 1:
				f = 1
				break
			else:
				l[j + 1][i] = 0
if f == 1:
	print(0)
else:
	p = sum((x.count(2) for x in l))
	print(pow(2, p) % 1000000007)


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

def sol():
	cells = [[None] * w for i in range(h)]
	for i in range(h):
		if R[i] == 0:
			cells[i][0] = 'E'
		else:
			for j in range(R[i]):
				cells[i][j] = 'F'
			j += 1
			if j < w:
				cells[i][j] = 'E'
	for j in range(w):
		if C[j] == 0:
			if cells[0][j] == 'F':
				return 0
			else:
				cells[0][j] = 'E'
		else:
			for i in range(C[j]):
				if cells[i][j] == 'E':
					return 0
				else:
					cells[i][j] = 'F'
			i += 1
			if i < h:
				if cells[i][j] == 'F':
					return 0
				else:
					cells[i][j] = 'E'
	count = 0
	for i in range(h):
		for j in range(w):
			if cells[i][j] == None:
				count += 1
	exp = 1000000007
	return pow(2, count, exp)
print(sol())


from sys import stdin, stdout
cin = stdin.readline
cout = stdout.write
(h, w) = map(int, cin().split())
r = list(map(int, cin().split()))
c = list(map(int, cin().split()))
for i in range(h):
	for j in range(r[i]):
		if c[j] == i:
			cout('0')
			exit(0)
	if r[i] + 1 <= w and c[r[i]] >= i + 1:
		cout('0')
		exit(0)
lst = [[0] * (w + 1) for _ in range(h + 1)]
for i in range(h):
	lst[i][:r[i] + 1] = [1] * (r[i] + 1)
for j in range(w):
	for m in range(c[j] + 1):
		lst[m][j] = 1
count = 0
for i in range(1, h):
	count += w - 1 - sum(lst[i][1:w])
cout(str(2 ** count % (10 ** 9 + 7)))


from collections import defaultdict as dc
import sys
import math

def inp():
	p = sys.stdin.readline()
	return p
(n, m) = list(map(int, inp().split()))
r = list(map(int, inp().split()))
c = list(map(int, inp().split()))
p = [[0 for i in range(m)] for j in range(n)]
ans = 0
kl = 10 ** 9 + 7
for i in range(n):
	for j in range(r[i]):
		p[i][j] = 1
for j in range(m):
	for i in range(c[j]):
		p[i][j] = 1
jh = 0
for i in range(n):
	l = 0
	for j in range(m):
		if p[i][j] == 0:
			break
		else:
			l = l + p[i][j]
	if l != r[i]:
		print(0)
		jh = 1
		break
if jh == 0:
	for i in range(m):
		l = 0
		for j in range(n):
			if p[j][i] == 0:
				break
			l = l + p[j][i]
		if l != c[i]:
			print(0)
			jh = 1
			break
if jh == 0:
	for i in range(1, n):
		for j in range(1, m):
			if p[i][j] != 1:
				z = p[i - 1][j]
				x = p[i][j - 1]
				if x == 1 and r[i] == j:
					continue
				if z == 1 and c[j] == i:
					continue
				ans += 1
	print(pow(2, ans, kl))


(height, width) = map(int, input().split())
hs = list(map(int, input().split()))
ws = list(map(int, input().split()))
mat = [[0 for x in range(width)] for y in range(height)]
for i in range(len(hs)):
	for j in range(min(width, hs[i])):
		mat[i][j] += 1
for i in range(len(ws)):
	for j in range(min(height, ws[i])):
		mat[j][i] += 1

def check():
	for i in range(len(hs)):
		if hs[i] < width:
			if mat[i][hs[i]] > 0:
				return False
	for i in range(len(ws)):
		if ws[i] < height:
			if mat[ws[i]][i] > 0:
				return False
	return True
if not check():
	print(0)
	exit()
for i in range(len(hs)):
	for j in range(min(width, hs[i] + 1)):
		mat[i][j] = 1
for i in range(len(ws)):
	for j in range(min(height, ws[i] + 1)):
		mat[j][i] = 1
count0 = 0
for i in mat:
	for num in i:
		if num == 0:
			count0 += 1

def printmat():
	for i in mat:
		print(i)
	print()
	print(count0)
resp = 0
if count0 != 0:
	resp = 2 ** count0 % 1000000007
else:
	resp = 1
print(resp)


import os
import sys
from io import BytesIO, IOBase

def solution():
	pass

def main():
	(w, h) = r_array()
	ww = r_array()
	hh = r_array()
	N = int(1000000000.0 + 7)
	res = [['-'] * (h + 2) for _ in range(w + 2)]
	for (i, d) in enumerate(ww, start=1):
		res[i][d + 1] = 0
		for j in range(1, d + 1):
			res[i][j] = 1
	for (i, d) in enumerate(hh, start=1):
		if res[d + 1][i] == 1:
			write(0)
			return
		res[d + 1][i] = 0
		for j in range(1, d + 1):
			if res[j][i] == 0:
				write(0)
				return
			res[j][i] = 1
	ans = sum((row[1:h + 1].count('-') for row in res[1:w + 1]))
	write(2 ** ans % N)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')

def write(*args, end='\n'):
	for x in args[:-1]:
		sys.stdout.write(str(x) + ' ')
	sys.stdout.write(str(args[-1]))
	sys.stdout.write(end)

def r_array():
	return [int(x) for x in input().split()]

def r_int():
	return int(input())
main()


(hw, _r, _c) = (input(), input(), input())
r_c = [int(x) for x in hw.split()]
r = [int(x) for x in _r.split()]
c = [int(x) for x in _c.split()]
i = 0
j = 0
final_pow = 0
k = False
while i < r_c[0]:
	if k == True:
		break
	while j < r_c[1]:
		if j > r[i] and i > c[j]:
			final_pow += 1
		if j < r[i] and i == c[j] or (i < c[j] and j == r[i]):
			k = True
			break
		j += 1
	i += 1
	j = 0
res = 2 ** final_pow % (10 ** 9 + 7)
if k:
	print(0)
else:
	print(res)


import sys

def findways(h, w, r, c, arr):
	for a in range(h):
		val = r[a]
		for b in range(min(val + 1, w)):
			if b == val and val + 1 <= w:
				arr[a][b] = -1
			else:
				arr[a][b] = 1
	for e in range(w):
		val2 = c[e]
		for f in range(min(val2 + 1, h)):
			if f == val2 and val2 + 1 <= h:
				if arr[f][e] == 1:
					return -1
				arr[f][e] = -1
			else:
				if arr[f][e] == -1:
					return -1
				arr[f][e] = 1
	count = 0
	for p in range(h):
		for q in range(w):
			if arr[p][q] == 0:
				count += 1
	return count
[h, w] = [int(i) for i in sys.stdin.readline().split()]
r = [int(i) for i in sys.stdin.readline().split()]
c = [int(i) for i in sys.stdin.readline().split()]
arr = []
for i in range(h):
	tmp = []
	for j in range(w):
		tmp.append(0)
	arr.append(tmp)
count = findways(h, w, r, c, arr)
if count == -1:
	print(0)
elif count == 0:
	print(1)
else:
	print(pow(2, count, pow(10, 9) + 7))


(h, w) = map(int, input().split())
arr_h = list(map(int, input().split()))
arr_w = list(map(int, input().split()))
k = 0
for i in range(h):
	m = 0
	t = 0
	for j in range(0, w):
		if j > arr_h[i] and i + 1 > arr_w[j] + 1:
			m += 1
		if j == arr_h[i] and i + 1 <= arr_w[j] or (j < arr_h[i] and i + 1 == arr_w[j] + 1):
			print(0)
			exit(0)
	k += m
print(pow(2, k, 1000000007))


(h, w) = map(int, input().split())
grid = [[-1 for i in range(w)] for j in range(h)]
numeros = list(map(int, input().split()))
for i in range(h):
	e = numeros[i]
	for j in range(e):
		grid[i][j] = 0
	if e < w:
		grid[i][e] = 1
numeros = list(map(int, input().split()))
flag = False
for i in range(w):
	e = numeros[i]
	for j in range(e):
		if grid[j][i] == 1:
			flag = True
		grid[j][i] = 0
	if e < h:
		if grid[e][i] == 0:
			flag = True
		grid[e][i] = 1
cont = 0
for i in range(h):
	for j in range(w):
		if grid[i][j] == -1:
			cont += 1
if flag:
	print(0)
else:
	print(pow(2, cont, 1000000007))


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log2, ceil
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
from bisect import insort
from collections import Counter
from collections import deque
from heapq import heappush, heappop, heapify
from itertools import permutations, combinations
from itertools import accumulate as ac
mod = int(1000000000.0) + 7
ip = lambda : int(stdin.readline())
inp = lambda : map(int, stdin.readline().split())
ips = lambda : stdin.readline().rstrip()
out = lambda x: stdout.write(str(x) + '\n')
t = 1
for _ in range(t):
	(n, m) = inp()
	r = list(inp())
	w = list(inp())
	arr = [[0] * m for i in range(n)]
	mark = [[False] * m for i in range(n)]
	for i in range(n):
		for j in range(r[i]):
			arr[i][j] += 1
			mark[i][j] = True
		cur = r[i]
		if cur <= m - 1:
			mark[i][cur] = True
	for i in range(m):
		for j in range(w[i]):
			arr[j][i] += 1
			mark[j][i] = True
		cur = w[i]
		if cur <= n - 1:
			mark[cur][i] = True
	f = True
	for i in range(n):
		cur = r[i]
		if cur <= m - 1:
			if arr[i][cur] != 0:
				f = False
	for i in range(m):
		cur = w[i]
		if cur <= n - 1:
			if arr[cur][i] != 0:
				f = False
	if not f:
		ans = 0
	else:
		ct = 0
		for i in mark:
			for j in i:
				if not j:
					ct += 1
		ans = pow(2, ct, mod)
	print(ans)


import sys
import itertools
(R, C) = map(int, sys.stdin.readline().rstrip().split())
r = list(map(int, sys.stdin.readline().rstrip().split()))
c = list(map(int, sys.stdin.readline().rstrip().split()))
table = [[-1] * C for _ in range(R)]
for (ind, ri) in enumerate(r):
	for k in range(ri):
		table[ind][k] = 1
	if ri < C:
		table[ind][ri] = 0
for (ind, ci) in enumerate(c):
	for k in range(ci):
		if table[k][ind] == 0:
			print(0)
			exit(0)
		table[k][ind] = 1
	if ci < R:
		if table[ci][ind] == 1:
			print(0)
			exit(0)
		table[ci][ind] = 0
free = list(itertools.chain.from_iterable(table)).count(-1)
print(pow(2, free, 10 ** 9 + 7))


import sys

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7
(H, W) = MAP()
R = LIST()
C = LIST()
grid = list2d(H, W, '*')
for i in range(H):
	r = R[i]
	for j in range(W):
		if r == j:
			grid[i][j] = 'W'
			break
		else:
			grid[i][j] = 'B'
for j in range(W):
	c = C[j]
	for i in range(H):
		if c == i:
			if grid[i][j] == 'B':
				print(0)
				exit()
			grid[i][j] = 'W'
			break
		else:
			if grid[i][j] == 'W':
				print(0)
				exit()
			grid[i][j] = 'B'
cnt = 0
for i in range(H):
	cnt += grid[i].count('*')
print(pow(2, cnt, MOD))


def get_var_cnt(h, w, r, c):
	table = list()
	for _ in range(h):
		row = list()
		for _ in range(w):
			row.append(-1)
		table.append(row)
	used_counter = 0
	for (ind, val) in enumerate(r):
		if val == 0:
			table[ind][0] = 0
			used_counter += 1
		else:
			for i in range(val):
				table[ind][i] = 1
				used_counter += 1
			if val != w:
				table[ind][val] = 0
				used_counter += 1
	for (ind, val) in enumerate(c):
		if val == 0 and table[0][ind] == 1:
			return 0
		if val == 0 and table[0][ind] == -1:
			table[0][ind] = 0
			used_counter += 1
		elif val != 0:
			for i in range(val):
				if table[i][ind] == -1:
					table[i][ind] = 1
					used_counter += 1
				elif table[i][ind] == 0:
					return 0
			if val != h:
				if table[val][ind] == 1:
					return 0
				elif table[val][ind] == -1:
					table[val][ind] = 0
					used_counter += 1
	MD = int(1000000000.0 + 7)
	return 2 ** (h * w - used_counter) % MD
(h, w) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
print(get_var_cnt(h, w, r, c))


(m, n) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
grid = [['ok'] * (n + 1) for i in range(m + 1)]
for i in range(m):
	row = r[i]
	if row:
		for j in range(row):
			grid[i][j] = 1
		grid[i][row] = 0
	else:
		grid[i][row] = 0
inv = False
for j in range(n):
	col = c[j]
	if col:
		for i in range(col):
			if grid[i][j] == 0:
				inv = True
				break
			else:
				grid[i][j] = 1
		if grid[col][j] == 1:
			inv = True
			break
		else:
			grid[col][j] = 0
	elif grid[col][j] == 1:
		inv = True
		break
	else:
		grid[col][j] = 0
if inv:
	print(0)
else:
	cnt = 0
	for row in grid[:m]:
		cnt += row[:n].count('ok')
	print(pow(2, cnt, 10 ** 9 + 7))


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)


(r, c) = map(int, input().split())
rs = list(map(int, input().split()))
cs = list(map(int, input().split()))
d = [[0] * c for _ in range(r)]
ans = 0
mod = 10 ** 9 + 7
for i in range(r):
	for j in range(min(rs[i], c)):
		d[i][j] = 1
	if rs[i] < c:
		d[i][rs[i]] = -1
for j in range(c):
	for i in range(min(cs[j], r)):
		if d[i][j] == -1:
			print(0)
			exit()
		d[i][j] = 1
	if cs[j] < r:
		if d[cs[j]][j] == 1:
			print(0)
			exit()
		d[cs[j]][j] = -1
for i in range(r):
	for j in range(c):
		if d[i][j] == 0:
			ans += 1
print((1 << ans) % mod)


from sys import stdin
(h, w) = list(map(int, stdin.readline().rstrip().split()))
R = list(map(int, stdin.readline().rstrip().split()))
C = list(map(int, stdin.readline().rstrip().split()))

def findval(i, j, R):
	if R[i] - 1 >= j:
		return 1
	elif R[i] == j:
		return 0
	else:
		return -1
flag = True
count = 0
for j in range(w):
	for i in range(h):
		if i <= C[j] - 1:
			if findval(i, j, R) == 0:
				flag = False
		elif i == C[j]:
			if findval(i, j, R) == 1:
				flag = False
		elif findval(i, j, R) == -1:
			count += 1
if not flag:
	print(0)
else:
	print(2 ** count % (10 ** 9 + 7))


(h, w) = map(int, input().split())
a = []
for i in range(h):
	a.append([-1] * w)
r = list(map(int, input().split()))
c = list(map(int, input().split()))
for i in range(h):
	if r[i] == w:
		for j in range(w):
			if a[i][j] == 0:
				print(0)
				exit(0)
			a[i][j] = 1
	else:
		for j in range(r[i]):
			if a[i][j] == 0:
				print(0)
				exit(0)
			a[i][j] = 1
		if a[i][r[i]] == 1:
			print(0)
			exit(0)
		a[i][r[i]] = 0
for i in range(w):
	if c[i] == h:
		for j in range(h):
			if a[j][i] == 0:
				print(0)
				exit(0)
			a[j][i] = 1
	else:
		for j in range(c[i]):
			if a[j][i] == 0:
				print(0)
				exit(0)
			a[j][i] = 1
		if a[c[i]][i] == 1:
			print(0)
			exit(0)
		a[c[i]][i] = 0
ans = 1
for i in range(h):
	for j in range(w):
		if a[i][j] == -1:
			ans *= 2
			ans %= 10 ** 9 + 7
print(ans)


(h, w) = [int(i) for i in input().split()]
rows = [int(i) for i in input().split()]
cols = [int(i) for i in input().split()]
data = [[0] * w for i in range(h)]
for i in range(h):
	r = rows[i]
	for j in range(r):
		data[i][j] = 2
	if r != w:
		data[i][r] = 1
for i in range(w):
	c = cols[i]
	for j in range(c):
		if data[j][i] == 1:
			print(0)
			exit()
		data[j][i] = 2
	if c != h:
		if data[c][i] == 2:
			print(0)
			exit()
		data[c][i] = 1
cnt = 0
for l in data:
	cnt += l.count(0)
print(pow(2, cnt, 1000000007))


(h, w) = map(int, input().split())
m = {}
for (x, my) in enumerate(map(int, input().split())):
	for y in range(my):
		m[x, y] = True
	if my < w:
		m[x, my] = False
for (y, mx) in enumerate(map(int, input().split())):
	for x in range(mx):
		if not m.get((x, y), True):
			print(0)
			exit()
		m[x, y] = True
	if mx < h:
		if m.get((mx, y), False):
			print(0)
			exit()
		m[mx, y] = False
print(2 ** (h * w - len(m)) % (10 ** 9 + 7))


(n, m) = map(int, input().split())
row = list(map(int, input().split()))
col = list(map(int, input().split()))
grid = [['?'] * m for _ in range(n)]
for i in range(n):
	lng = row[i]
	for j in range(lng):
		grid[i][j] = 'b'
	if lng < m:
		grid[i][lng] = 'w'
for i in range(m):
	lng = col[i]
	for j in range(lng):
		if grid[j][i] != 'w':
			grid[j][i] = 'b'
		else:
			print(0)
			exit(0)
	if lng < n:
		if grid[lng][i] != 'b':
			grid[lng][i] = 'w'
		else:
			print(0)
			exit(0)
comb = 1
for _ in range(sum(map(lambda l: l.count('?'), grid))):
	comb = comb * 2 % 1000000007
print(comb)


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(1000000000.0 + 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
for j in range(c):
	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(pow(2, cnt, MOD))


(n, m) = map(int, input().split())
a = []
for i in range(n):
	a.append([0] * m)
r = list(map(int, input().split()))
c = list(map(int, input().split()))
k = n * m
for i in range(n):
	for j in range(m):
		if j < r[i]:
			if a[i][j] == 2:
				print(0)
				exit()
			else:
				if a[i][j] == 0:
					k -= 1
				a[i][j] = 1
		elif j == r[i]:
			if a[i][j] == 1:
				print(0)
				exit()
			else:
				if a[i][j] == 0:
					k -= 1
				a[i][j] = 2
		if i < c[j]:
			if a[i][j] == 2:
				print(0)
				exit()
			else:
				if a[i][j] == 0:
					k -= 1
				a[i][j] = 1
		elif i == c[j]:
			if a[i][j] == 1:
				print(0)
				exit()
			else:
				if a[i][j] == 0:
					k -= 1
				a[i][j] = 2
print(2 ** k % 1000000007)


mod = 10 ** 9 + 7

def solve(h, w, r, c):
	grid = [[0 for i in range(w)] for i in range(h)]
	for i in range(h):
		for j in range(min(r[i], w)):
			grid[i][j] = 1
		if r[i] < w:
			grid[i][r[i]] = -1
	for i in range(w):
		for j in range(min(h, c[i])):
			if grid[j][i] == -1:
				return 0
			grid[j][i] = 1
		if c[i] < h and grid[c[i]][i] == 1:
			return 0
		if c[i] < h:
			grid[c[i]][i] = -1
	count = 0
	for i in range(h):
		for j in range(w):
			if grid[i][j] == 0:
				count += 1
	return pow(2, count, mod)
(h, w) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
print(solve(h, w, r, c))


(h, w) = [int(i) for i in str.split(input())]
r = [int(i) for i in str.split(input())]
c = [int(i) for i in str.split(input())]
m = [[None for i in range(w)] for j in range(h)]

def f():
	for rr in range(h):
		for cc in range(r[rr]):
			if m[rr][cc] == False:
				print(0)
				return
			m[rr][cc] = True
		if r[rr] < w:
			if m[rr][r[rr]] == True:
				print(0)
				return
			m[rr][r[rr]] = False
	for cc in range(w):
		for rr in range(c[cc]):
			if m[rr][cc] == False:
				print(0)
				return
			m[rr][cc] = True
		if c[cc] < h:
			if m[c[cc]][cc] == True:
				print(0)
				return
			m[c[cc]][cc] = False
	none = sum([1 for i in range(w) for j in range(h) if m[j][i] is None])
	print(pow(2, none, 1000000007))
f()


(h, w) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
mat = [[0 for i in range(w)] for j in range(h)]
row = 0
for cell in r:
	col = 0
	for i in range(cell):
		mat[row][col] = 1
		col += 1
	if col < w:
		mat[row][col] = -1
	row += 1
col = 0
for cell in c:
	row = 0
	for i in range(cell):
		if mat[row][col] == -1:
			print(0)
			exit()
		mat[row][col] = 1
		row += 1
	if row < h:
		if mat[row][col] == 1:
			print(0)
			exit()
		mat[row][col] = -1
	col += 1
count = 0
for i in range(h):
	for j in range(w):
		if mat[i][j] == 0:
			count += 1

def power(x, y, p):
	res = 1
	x = x % p
	while y > 0:
		if y & 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res
a = 2
b = str(count)
remainderB = 0
MOD = 1000000007
for i in range(len(b)):
	remainderB = (remainderB * 10 + ord(b[i]) - 48) % (MOD - 1)
print(power(a, remainderB, MOD))


(h, w) = 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)
			exit(0)
		table[i][j] = 1
	if col < h:
		if table[col][j] == 1:
			print(0)
			exit(0)
		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)


from math import factorial
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
u = []
for i in range(n):
	u.append([0] * m)
for i in range(n):
	for k in range(a[i]):
		if u[i][k] == 2:
			print(0)
			exit()
		u[i][k] = 1
	if a[i] < m:
		if u[i][a[i]] == 1:
			print(0)
			exit()
		u[i][a[i]] = 2
for j in range(m):
	for k in range(b[j]):
		if u[k][j] == 2:
			print(0)
			exit()
		u[k][j] = 1
	if b[j] < n:
		if u[b[j]][j] == 1:
			print(0)
			exit()
		u[b[j]][j] = 2
ans = 0
for i in range(n):
	for j in range(m):
		if u[i][j] == 0:
			ans += 1
q = int(1000000000.0 + 7)
a1 = pow(2, ans, q)
print(a1)


import sys
input = sys.stdin.readline
from math import log2, log, ceil

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def nCr(n, k):
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return res

def upper_bound(a, x, lo=0):
	hi = len(a)
	while lo < hi:
		mid = (lo + hi) // 2
		if a[mid] < x:
			lo = mid + 1
		else:
			hi = mid
	return lo

def primefs(n):
	primes = {}
	while n % 2 == 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	while x != link[x]:
		x = link[x]
	return x

def union(x, y, size, link):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

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

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

def int_array():
	return list(map(int, input().strip().split()))

def str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
(h, w) = int_array()
r = int_array()
c = int_array()
grid = [[None for _ in range(w)] for __ in range(h)]
f = 1
for i in range(h):
	last = -1
	for j in range(r[i]):
		grid[i][j] = 1
		last = j
	if last + 1 < w:
		grid[i][last + 1] = 0
for i in range(w):
	last = -1
	for j in range(c[i]):
		if grid[j][i] == 0:
			f = 0
			break
		grid[j][i] = 1
		last = j
	if last + 1 < h:
		if grid[last + 1][i] == 1:
			f = 0
			break
		grid[last + 1][i] = 0
if f:
	ans = 0
	for i in grid:
		ans += i.count(None)
	ans = power(2, ans, MOD)
	print(ans % MOD)
else:
	print(0)


from math import factorial

def pm(mm, h, w):
	print('--------')
	for i in range(h):
		for j in range(w):
			print(mm[i][j], end=' ')
		print('\n', end='')

def solve():
	(h, w) = list(map(int, input().split(' ')))
	l = list(map(int, input().split(' ')))
	c = list(map(int, input().split(' ')))
	mm = []
	for i in range(h):
		if l[i] == 0:
			mm.append([0] + ['u'] * (w - 1))
		else:
			mm.append([1] * l[i] + [0] + ['u'] * (w - l[i] + 1))
	for i in range(w):
		if c[i] == 0:
			if mm[0][i] == 1:
				return 0
			mm[0][i] = 0
		else:
			for j in range(c[i]):
				if mm[j][i] == 0:
					return 0
				mm[j][i] = 1
				if j == c[i] - 1 and c[i] < h:
					if mm[c[i]][i] == 1:
						return 0
					mm[c[i]][i] = 0
	u_c = 0
	for i in range(h):
		for j in range(w):
			if mm[i][j] == 'u':
				u_c += 1
	if u_c == 0:
		return 1
	else:
		return 2 ** u_c % 1000000007
print(solve())


(h, w) = [int(x) for x in input().split()]
r = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]
mat = [['X' for i in range(w + 1)] for j in range(h + 1)]

def assign(r, c, t):
	if mat[r][c] == 'X':
		mat[r][c] = t
	elif mat[r][c] != t:
		raise ValueError

def start():
	try:
		for row in range(h):
			for col in range(r[row]):
				assign(row, col, 'FULL')
			assign(row, r[row], 'Emp')
		for col in range(w):
			for row in range(c[col]):
				assign(row, col, 'FULL')
			assign(c[col], col, 'Emp')
	except ValueError:
		return 0
	res = 1
	for row in range(h):
		for col in range(w):
			if mat[row][col] == 'X':
				res = res * 2 % (10 ** 9 + 7)
	return res
print(start())


(h, w) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
a = [[0 for _ in range(w + 1)] for _ in range(h + 1)]
kt = True
for i in range(0, h):
	for j in range(0, r[i]):
		if a[i][j] == 2:
			if kt:
				print(0)
			kt = False
		else:
			a[i][j] = 1
	if a[i][r[i]] == 1:
		if kt:
			print(0)
		kt = False
	else:
		a[i][r[i]] = 2
for j in range(0, w):
	for i in range(0, c[j]):
		if a[i][j] == 2:
			if kt:
				print(0)
			kt = False
		else:
			a[i][j] = 1
	if a[c[j]][j] == 1:
		if kt:
			print(0)
		kt = False
	else:
		a[c[j]][j] = 2
dem = 0
for i in range(0, h):
	for j in range(0, w):
		if a[i][j] == 0:
			dem += 1
s = 1
for i in range(1, dem + 1):
	s = s * 2 % 1000000007
if kt:
	print(s)


(n, m) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
a = [[1 for x in range(m)] for x in range(n)]
for i in range(n):
	for j in range(r[i]):
		a[i][j] = 0
	if r[i] < m:
		a[i][r[i]] = 2
ok = 1
for j in range(m):
	for i in range(c[j]):
		if a[i][j] == 2:
			ok = 0
		a[i][j] = 0
	if c[j] < n:
		if a[c[j]][j] == 0:
			ok = 0
		a[c[j]][j] = 2
cnt = 0
for i in range(n):
	for j in range(m):
		cnt += a[i][j] == 1
if ok:
	print(2 ** cnt % int(1000000000.0 + 7))
else:
	print(0)


(n, m) = map(int, input().split())
r = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]
mat = []
const = 10 ** 9 + 7
for i in range(n):
	temp = [-1] * m
	mat.append(temp)
f = 0
for i in range(n):
	if r[i] > m:
		f = 1
		break
	if r[i] == 0:
		mat[i][0] = 0
	else:
		for j in range(r[i]):
			mat[i][j] = 1
		if r[i] + 1 <= m:
			mat[i][r[i]] = 0
if f == 1:
	print(0)
else:
	f = 0
	for i in range(m):
		if c[i] > n:
			f = 1
			break
		if c[i] == 0:
			if mat[0][i] == 1:
				f = 1
				break
			else:
				mat[0][i] = 0
		else:
			for j in range(c[i]):
				if mat[j][i] == 0:
					f = 1
					break
				mat[j][i] = 1
			if f == 1:
				break
			if c[i] + 1 <= n:
				if mat[c[i]][i] == 1:
					f = 1
				else:
					mat[c[i]][i] = 0
		if f == 1:
			break
	if f == 1:
		print(0)
	else:
		count = 0
		for i in range(n):
			for j in range(m):
				if mat[i][j] == -1:
					count += 1
		print(2 ** count % const)


ii = lambda : int(input())
kk = lambda : map(int, input().split())
ll = lambda : list(kk())

def pow(base, exp):
	if exp == 0:
		return 1
	if exp == 1:
		return base
	res = pow(base, exp // 2)
	res *= res
	if exp & 1:
		res *= base
	return res % mod
(h, w) = kk()
(hs, ws) = (kk(), kk())
grid = [[-1] * w for _ in range(h)]
for (i, hv) in enumerate(hs):
	for j in range(hv):
		grid[i][j] = 1
	if hv < w:
		grid[i][hv] = 0
for (j, wv) in enumerate(ws):
	for i in range(wv):
		if grid[i][j] == 0:
			print(0)
			exit()
		grid[i][j] = 1
	if wv < h:
		if grid[wv][j] == 1:
			print(0)
			exit()
		grid[wv][j] = 0
c = 0
for i in range(h):
	for j in range(w):
		if grid[i][j] == -1:
			c += 1
ans = 1
mod = 10 ** 9 + 7
print(pow(2, c))


import sys
import os
import math

def READ(fileName):
	sys.stdin = open('in.txt')
while True:
	try:
		(n, m) = map(int, input().split())
	except:
		break
	ns = list(map(int, input().split()))
	ms = list(map(int, input().split()))
	mat = [[-1 for x in range(m)] for y in range(n)]
	for i in range(n):
		now = ns[i]
		if now == 0:
			mat[i][0] = 0
		else:
			for j in range(0, now, 1):
				mat[i][j] = 1
			if now < m:
				mat[i][now] = 0
	isValid = True
	for i in range(m):
		now = ms[i]
		if now == 0:
			if mat[0][i] == 1:
				isValid = False
				break
			else:
				mat[0][i] = 0
		else:
			for j in range(0, now, 1):
				if mat[j][i] == 0:
					isValid = False
					break
				else:
					mat[j][i] = 1
			if now < n:
				if mat[now][i] == 1:
					isValid = False
				else:
					mat[now][i] = 0
	if isValid == False:
		print('0')
	else:
		unreserved = 0
		for row in mat:
			unreserved += row.count(-1)
		ans = pow(2, unreserved, 1000000007)
		print(ans)
pass


from sys import stdin
(h, w) = map(int, stdin.readline().split())
r = list(map(int, stdin.readline().split()))
c = list(map(int, stdin.readline().split()))
dp = [['d' for i in range(w)] for j in range(h)]
flag = 0
cnt = 0
for i in range(h):
	for j in range(r[i]):
		dp[i][j] = 'f'
	if r[i] < w:
		dp[i][r[i]] = 'e'
for i in range(w):
	for j in range(c[i]):
		if flag == 1:
			break
		if dp[j][i] == 'e':
			flag = 1
		else:
			dp[j][i] = 'f'
	if c[i] < h:
		if dp[c[i]][i] == 'f':
			flag = 1
		else:
			dp[c[i]][i] = 'e'
if flag == 1:
	print(0)
else:
	for i in range(h):
		cnt += dp[i].count('d')
	print(2 ** cnt % 1000000007)


(h, w) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
res = 1
M = 1000000007
for i in range(w):
	for j in range(h):
		if j > c[i] and i > r[j]:
			res = res * 2 % M
		if j < c[i] and i == r[j]:
			print(0)
			exit(0)
		if j == c[i] and i < r[j]:
			print(0)
			exit(0)
print(res)


(h, w) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
m = [[0 for _ in range(w)] for _ in range(h)]
for i in range(h):
	for j in range(w):
		if j < r[i]:
			m[i][j] = 1
		elif j == r[i]:
			m[i][j] = -1
		elif j > r[i]:
			break
for j in range(w):
	f = False
	for i in range(h):
		if i < c[j]:
			if m[i][j] < 0:
				print(0)
				f = True
				break
			m[i][j] = 1
		elif i == c[j]:
			if m[i][j] > 0:
				print(0)
				f = True
				break
			m[i][j] = -1
		elif j > r[i]:
			break
	if f:
		exit(0)
acc = 0
for i in range(h):
	for j in range(w):
		acc += 1 if m[i][j] == 0 else 0
print(2 ** acc % (10 ** 9 + 7))


import sys
(h, w) = [int(i) for i in input().split(' ')]
r = [int(i) for i in input().split(' ')]
c = [int(i) for i in input().split(' ')]
grid = [[None for j in range(w)] for i in range(h)]
n = h * w
for (i, ri) in enumerate(r):
	for j in range(0, ri):
		if grid[i][j] is None:
			n -= 1
			grid[i][j] = 1
		elif grid[i][j] == 0:
			print(0)
			exit(0)
	j = ri
	if j < w:
		if grid[i][j] is None:
			n -= 1
			grid[i][j] = 0
		elif grid[i][j] == 1:
			print(0)
			exit(0)
for (j, cj) in enumerate(c):
	for i in range(0, cj):
		if grid[i][j] is None:
			n -= 1
			grid[i][j] = 1
		elif grid[i][j] == 0:
			print(0)
			exit(0)
	i = cj
	if i < h:
		if grid[i][j] is None:
			n -= 1
			grid[i][j] = 0
		elif grid[i][j] == 1:
			print(0)
			exit(0)
res = 2 ** n % 1000000007
print(res)


from sys import stdin, exit

def myassert(condition):
	if not condition:
		print(0)
		exit(0)

def main():
	data = stdin.read().split('\n')
	(h, w) = map(int, data[0].split(' '))
	rows = list(map(int, data[1].split(' ')))
	cols = list(map(int, data[2].split(' ')))
	assert len(rows) == h
	assert len(cols) == w
	x = [w * [2] for x in range(h)]
	for i in range(h):
		r = rows[i]
		for j in range(r):
			myassert(x[i][j] != 1)
			x[i][j] = 0
		if r < w:
			myassert(x[i][r] != 0)
			x[i][r] = 1
	for i in range(w):
		c = cols[i]
		for j in range(c):
			myassert(x[j][i] != 1)
			x[j][i] = 0
		if c < h:
			myassert(x[c][i] != 0)
			x[c][i] = 1
	s = 0
	for i in range(h):
		for j in range(w):
			if x[i][j] == 2:
				s += 1
	print(pow(2, s, 1000000007))
main()


(h, w) = map(int, input().split())
hr = list(map(int, input().split()))
hc = list(map(int, input().split()))
count = 0
flag = 0
for i in range(h):
	for j in range(w):
		if j > hr[i] and i > hc[j]:
			count += 1
		if j == hr[i] and i < hc[j]:
			flag = 1
			break
		if j < hr[i] and i == hc[j]:
			flag = 1
			break
	if flag == 1:
		break
if flag == 1:
	print(0)
else:
	print(pow(2, count, 1000000007))


X = list(map(int, input().split()))
Array = [['blank'] * X[1] for i in range(X[0])]
Row = list(map(int, input().split()))
Column = list(map(int, input().split()))
for i in range(X[0]):
	for j in range(0, Row[i]):
		Array[i][j] = 'Set'
	if Row[i] != X[1]:
		Array[i][Row[i]] = 'Last'
for i in range(X[1]):
	for j in range(0, Column[i]):
		if Array[j][i] == 'Last':
			print(0)
			exit()
		Array[j][i] = 'Set'
	if Column[i] != X[0]:
		if Array[Column[i]][i] == 'Set':
			print(0)
			exit()
		Array[Column[i]][i] = 'Last'
SUM = 0
for i in range(X[0]):
	SUM += Array[i].count('blank')
Answer = 1
for i in range(1, SUM + 1):
	Answer *= 2
	Answer = Answer % 1000000007
print(Answer)


a = input().split()
h = int(a[0])
w = int(a[1])
R = list(map(int, input().split()))
C = list(map(int, input().split()))
mod = 10 ** 9 + 7
fl = 0
cord = [[2] * w for _ in range(h)]
for i in range(w):
	if C[i] == 0:
		cord[0][i] = 0
	else:
		cord[0][i] = 1
for i in range(h):
	if R[i] == 0:
		cord[i][0] = 0
	else:
		cord[i][0] = 1
for y in range(h):
	if fl == 1:
		break
	for x in range(w):
		if C[x] > y:
			if cord[y][x] == 0:
				fl = 1
				break
			else:
				cord[y][x] = 1
		elif C[x] == y:
			if cord[y][x] == 1:
				fl = 1
				break
			else:
				cord[y][x] = 0
		if R[y] > x:
			if cord[y][x] == 0:
				fl = 1
				break
			else:
				cord[y][x] = 1
		elif R[y] == x:
			if cord[y][x] == 1:
				fl = 1
				break
			else:
				cord[y][x] = 0
if fl == 1:
	print(0)
else:
	ans = 0
	for y in range(h):
		ans += cord[y].count(2)
	print(2 ** ans % mod)


prime = 1000000007
t = 1
for test in range(t):
	(h, w) = list(map(int, input().split()))
	r = list(map(int, input().split()))
	c = list(map(int, input().split()))
	arr = [[-1 for i in range(w)] for i in range(h)]
	ind = 0
	err = 0
	for i in r:
		for j in range(i):
			arr[ind][j] = 1
		if i < w:
			arr[ind][i] = 0
		ind += 1
	ind = 0
	for cw in c:
		for i in range(cw):
			if arr[i][ind] == 0:
				err = 1
			else:
				arr[i][ind] = 1
		if cw < h:
			if arr[cw][ind] == 1:
				err = 1
			else:
				arr[cw][ind] = 0
		ind += 1
	if err == 1:
		print(0)
	else:
		count = 0
		for i in range(h):
			for j in range(w):
				if arr[i][j] == -1:
					count += 1
		print(pow(2, count, prime))


(h, w) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
dp = [[2 for _ in range(w)] for _ in range(h)]
ans = 1
for i in range(h):
	for j in range(r[i]):
		dp[i][j] = 1
	if r[i] < w:
		dp[i][r[i]] = 0
for j in range(w):
	for i in range(c[j]):
		if dp[i][j] == 2:
			dp[i][j] = 1
		elif dp[i][j] == 0:
			ans = 0
	if c[j] < h:
		if dp[c[j]][j] == 2:
			dp[c[j]][j] = 0
		elif dp[c[j]][j] == 1:
			ans = 0
mod = 10 ** 9 + 7
for i in range(h):
	for j in range(w):
		if dp[i][j] == 2:
			ans *= 2
			ans %= mod
print(ans)


(h, w) = map(int, input().split())
rows = [int(i) for i in input().split()]
cols = [int(i) for i in input().split()]
count = 0
grid = [[2 for i in range(w)] for j in range(h)]
works = True
mod = 1000000007
for i in range(h):
	for j in range(w):
		if i < cols[j] and grid[i][j] == 2:
			grid[i][j] = 1
		elif i < cols[j]:
			works = False
			break
		if i == cols[j] and grid[i][j] == 2:
			grid[i][j] = 0
		elif i == cols[j]:
			works = False
			break
		if j < rows[i] and grid[i][j] != 0:
			grid[i][j] = 1
		elif j < rows[i]:
			works = False
			break
		if j == rows[i] and grid[i][j] != 1:
			grid[i][j] = 0
		elif j == rows[i]:
			works = False
			break
if works:
	for i in range(h):
		for j in range(w):
			if grid[i][j] == 2:
				count += 1
	print(2 ** count % mod)
else:
	print(0)


(h, w) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
a = []
f = 0
for i in range(h):
	a.append([-1] * w)
for i in range(h):
	for j in range(r[i]):
		if a[i][j] != 0:
			a[i][j] = 1
		else:
			f = 1
	if a[i][min(r[i], w - 1)] != 1:
		a[i][min(r[i], w - 1)] = 0
	elif r[i] < w:
		f = 1
for i in range(w):
	for j in range(c[i]):
		if a[j][i] != 0:
			a[j][i] = 1
		else:
			f = 1
	if a[min(c[i], h - 1)][i] != 1:
		a[min(c[i], h - 1)][i] = 0
	elif c[i] < h:
		f = 1
if f:
	print(0)
else:
	cnt = 0
	for i in range(h):
		cnt += a[i].count(-1)
	print(pow(2, cnt, 10 ** 9 + 7))


mod = 1000000007
(h, w) = map(int, input().split())
x = [[2] * (w + 1) for _ in range(h + 1)]
r = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]
for i in range(h):
	if r[i] == 0:
		x[i][0] = 0
	else:
		for j in range(r[i]):
			x[i][j] = 1
		x[i][r[i]] = 0
for i in range(w):
	if c[i] == 0 and x[0][i] == 1:
		print(0)
		exit()
	elif c[i] == 0:
		x[0][i] = 0
	else:
		for j in range(c[i]):
			if x[j][i] == 0:
				print(0)
				exit()
			else:
				x[j][i] = 1
		if x[c[i]][i] == 1:
			print(0)
			exit()
		else:
			x[c[i]][i] = 0
ans = 0
for i in range(h):
	for j in range(w):
		ans += x[i][j] == 2
print(pow(2, ans, mod))


def power(count):
	delta = 2
	if count == 0:
		return 1
	for i in range(count - 1):
		delta *= 2
		delta = delta % 1000000007
	return delta

def func():
	specs = input().split()
	for i in range(2):
		specs[i] = int(specs[i])
	rows = specs[0]
	cols = specs[1]
	grid = [[0] * cols for i in range(rows)]
	ri = input().split()
	for i in range(len(ri)):
		ri[i] = int(ri[i])
	for i in range(rows):
		delta = 0
		for j in range(ri[i]):
			delta += 1
			grid[i][j] = 1
		if delta < cols:
			grid[i][delta] = -1
	ci = input().split()
	for i in range(len(ci)):
		ci[i] = int(ci[i])
	for j in range(cols):
		delta = 0
		for i in range(ci[j]):
			delta += 1
			if grid[i][j] == 1:
				continue
			if grid[i][j] == 0:
				grid[i][j] = 1
			elif grid[i][j] == -1:
				return 0
		if delta < rows:
			if grid[delta][j] == 0 or grid[delta][j] == -1:
				grid[delta][j] = -1
			else:
				return 0
	count = 0
	for i in range(rows):
		for j in range(cols):
			if grid[i][j] == 0:
				count += 1
	t = power(count)
	return t % 1000000007
x = func()
print(x)


(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)
			exit()
		continue
	for i in range(b[j]):
		if grid[i][j] != 0:
			grid[i][j] = 1
		else:
			print(0)
			exit()
	if 0 <= b[j] < h:
		if grid[b[j]][j] != 1:
			grid[b[j]][j] = 0
		else:
			print(0)
			exit()
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) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
mod = 1000000007
dp = [[0] * w for _ in range(h)]
for i in range(h):
	if r[i] == 0:
		dp[i][0] = -1
	else:
		for j in range(r[i]):
			dp[i][j] = 1
		if j + 1 < w:
			dp[i][j + 1] = -1
tf = True
for j in range(w):
	if c[j] == 0:
		if dp[0][j] == 1:
			tf = False
			break
		elif dp[0][j] == -1:
			pass
		else:
			dp[0][j] = -1
	else:
		for i in range(c[j]):
			if dp[i][j] == -1:
				tf = False
				break
			dp[i][j] = 1
		if i + 1 < h:
			if dp[i + 1][j] == 1:
				tf = False
				break
			dp[i + 1][j] = -1
if tf:
	ans = 0
	for i in range(h):
		for j in range(w):
			if dp[i][j] == 0:
				ans += 1
	print(pow(2, ans) % mod)
else:
	print(0)


from itertools import accumulate, chain
BLACK = 1
WHITE = 0
(height, weight) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
table = [[None] * weight for _ in range(height)]

def paint(x, y, color) -> bool:
	global table
	if table[x][y] is None:
		table[x][y] = color
		return True
	elif table[x][y] == color:
		return True
	else:
		return False
for x in range(height):
	for y in range(r[x]):
		paint(x, y, BLACK)
	if r[x] < weight:
		paint(x, r[x], WHITE)
exist = True
for y in range(weight):
	for x in range(c[y]):
		if not paint(x, y, BLACK):
			exist = False
			break
	if c[y] < height:
		if not paint(c[y], y, WHITE):
			exist = False
			break
if exist:
	not_used_cells = 0
	for i in chain(*table):
		if i is None:
			not_used_cells += 1
	print(pow(2, not_used_cells, int(1000000000.0 + 7)))
else:
	print(0)


(h, w) = map(int, input().split())
jhand = 0
llllll = [0] * w
hjgjyhvu = [[-1] * w for i in range(h)]
jhand = 0
llllll[0] = 1
andugundu = [int(o) for o in input().split()]
jhand = 0
for i in range(h):
	jhand = 0
	for j in range(andugundu[i]):
		jhand = 0
		hjgjyhvu[i][j] = 1
		jhand = 0
	if andugundu[i] < w:
		jhand = 0
		llllll[0] = 1
		hjgjyhvu[i][andugundu[i]] = 0
		jhand = 0
andugundu = [int(o) for o in input().split()]
jhand = 0
llllll[0] = 1
idkidk = 0
jhand = 0
llllll[0] = 1
for i in range(w):
	jhand = 0
	llllll[0] = 1
	for j in range(andugundu[i]):
		jhand = 0
		llllll[0] = 1
		if hjgjyhvu[j][i] != 0:
			jhand = 0
			hjgjyhvu[j][i] = 1
			jhand = 0
		else:
			jhand = 0
			idkidk = 1
			jhand = 0
		llllll[0] = 1
	if andugundu[i] < h:
		jhand = 0
		if hjgjyhvu[andugundu[i]][i] == 0 or hjgjyhvu[andugundu[i]][i] == -1:
			jhand = 0
			hjgjyhvu[andugundu[i]][i] = 0
			jhand = 0
		else:
			jhand = 0
			idkidk = 1
			jhand = 0
zz = 0
for i in range(h):
	zz += hjgjyhvu[i].count(-1)
	llllll[0] = 1
if idkidk == 0:
	llllll[0] = 1
	print(pow(2, zz, 1000000007))
	llllll[0] = 1
else:
	print('0')
	llllll[0] = 1


(h, w) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
n = 0
impossible = False
for i in range(h):
	if r[i] < w and c[r[i]] > i:
		impossible = True
for j in range(w):
	if c[j] < h and r[c[j]] > j:
		impossible = True
if not impossible:
	for i in range(h):
		for j in range(w):
			if r[i] < j and c[j] < i:
				n += 1
if impossible:
	print(0)
else:
	print(2 ** n % 1000000007)


(h, w) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
ans = 1
f = 0
M = 1000000007
for i in range(h):
	if f == 1:
		break
	for j in range(w):
		if i == c[j] and j < r[i] or (i < c[j] and j == r[i]):
			print(0)
			f = 1
			break
		if i > c[j] and j > r[i]:
			ans = ans * 2 % M
if f == 0:
	print(ans % M)


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


x = input()
x = x.split()
n = int(x[0])
m = int(x[1])
x = input()
r = x.split()
x = input()
c = x.split()
stop = 0
sum = 0
for j in range(n):
	if int(r[j]) < m:
		if int(c[int(r[j])]) >= j + 1:
			stop = 1
			break
	if m - int(r[j]) > 0:
		x = m - int(r[j]) - 1
	else:
		x = m - int(r[j])
	for i in range(m):
		if j == 0:
			if int(c[i]) < n:
				if int(r[int(c[i])]) >= i + 1:
					stop = 1
					break
		if int(c[i]) >= j and i > int(r[j]):
			x -= 1
		elif (i == 0 or j == 0) and i > int(r[j]):
			x -= 1
		if stop == 1:
			break
	sum += x
if stop == 0:
	print(str(2 ** sum % 1000000007))
else:
	print(0)


(h, w) = map(int, input().split())
r = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]
matrix = [[None] * w for i in range(h)]
for i in range(len(r)):
	for j in range(r[i]):
		if matrix[i][j] is None or matrix[i][j] == True:
			matrix[i][j] = True
		else:
			print(0)
			exit()
	if r[i] < w:
		if matrix[i][r[i]] is None or matrix[i][r[i]] == False:
			matrix[i][r[i]] = False
		else:
			print(0)
			exit()
for i in range(len(c)):
	for j in range(c[i]):
		if matrix[j][i] is None or matrix[j][i] == True:
			matrix[j][i] = True
		else:
			print(0)
			exit()
	if c[i] < h:
		if matrix[c[i]][i] is None or matrix[c[i]][i] == False:
			matrix[c[i]][i] = True
		else:
			print(0)
			exit()
answer = 1
for i in range(h):
	for j in range(w):
		if matrix[i][j] is None:
			answer = answer * 2 % 1000000007
print(answer)


(h, w) = map(int, input().split())
row_arr = list(map(int, input().split()))
col_arr = list(map(int, input().split()))
row_arr = [0] + row_arr
col_arr = [0] + col_arr
ans = 1
mod = 10 ** 9 + 7
for i in range(1, h + 1):
	for j in range(1, w + 1):
		if j <= row_arr[i] + 1 or i <= col_arr[j] + 1:
			pass
		else:
			ans = ans % mod * 2 % mod
for i in range(1, h + 1):
	for j in range(1, w + 1):
		if row_arr[i] - (j - 1) == 0 and i - 1 - col_arr[j] < 0:
			print(0)
			exit()
		if col_arr[j] - (i - 1) == 0 and j - 1 - row_arr[i] < 0:
			print(0)
			exit()
print(ans)


(n, m) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
a = [[-1] * m for _ in range(n)]
ans = 1
mod = 10 ** 9 + 7
for i in range(n):
	j = 0
	while j < r[i]:
		a[i][j] = 1
		j += 1
	if j < m:
		a[i][j] = 0
for j in range(m):
	i = 0
	while i < c[j]:
		if a[i][j] != -1 and a[i][j] != 1:
			ans = 0
		a[i][j] = 1
		i += 1
	if i < n:
		if a[i][j] != -1 and a[i][j] != 0:
			ans = 0
		a[i][j] = 0
for i in range(n):
	for j in range(m):
		if a[i][j] == -1:
			ans = ans * 2 % mod
print(ans)


import collections

def main():
	(h, w) = map(int, input().split())
	grid = [[None for _ in range(w)] for _ in range(h)]
	r = map(int, input().split())
	c = map(int, input().split())
	for (i, v) in enumerate(r):
		for j in range(v):
			grid[i][j] = 1
		if v < w:
			grid[i][v] = 0
	for (i, v) in enumerate(c):
		for j in range(v):
			if grid[j][i] == 0:
				print(0)
				return
			grid[j][i] = 1
		if v < h:
			if grid[v][i] == 1:
				print(0)
				return
			grid[v][i] = 0
	flex = sum([g.count(None) for g in grid])
	if flex == 0:
		print(1)
		return
	ans = 1
	for i in range(flex):
		ans = ans * 2 % 1000000007
	print(ans)
	return
main()


import sys
input = lambda : sys.stdin.readline().strip()
(m, n) = map(int, input().split())
ls1 = list(map(int, input().split()))
ls2 = list(map(int, input().split()))
matrix = []
for i in range(m):
	matrix.append([])
	for j in range(n):
		matrix[-1].append('-1')
for i in range(m):
	j = -1
	for j in range(ls1[i]):
		if matrix[i][j] == 'w':
			print(0)
			exit()
		matrix[i][j] = 'b'
	j += 1
	try:
		if matrix[i][j] == 'b':
			print(0)
			exit()
		matrix[i][j] = 'w'
	except SystemExit:
		exit()
	except:
		pass
for j in range(n):
	i = -1
	for i in range(ls2[j]):
		if matrix[i][j] == 'w':
			print(0)
			exit()
		matrix[i][j] = 'b'
	i += 1
	try:
		if matrix[i][j] == 'b':
			print(0)
			exit()
		matrix[i][j] = 'w'
	except SystemExit:
		exit()
	except:
		pass
cnt = 0
for i in range(m):
	for j in range(n):
		if matrix[i][j] == '-1':
			cnt += 1
print(pow(2, cnt, 10 ** 9 + 7))


(a, b) = map(int, input().split())
z = list(map(int, input().split()))
z1 = list(map(int, input().split()))
ans = 1
for i in range(a):
	for j in range(b):
		if i < z1[j] and j == z[i] or (i == z1[j] and j < z[i]):
			ans = 0
		elif i > z1[j] and j > z[i]:
			ans *= 2
			ans %= 1000000007
print(ans)


(h, w) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
mat = [[0 for i in range(w)] for j in range(h)]
for i in range(h):
	for j in range(r[i]):
		mat[i][j] = 1
for i in range(w):
	for j in range(c[i]):
		mat[j][i] = 1
z = 1
for i in range(h):
	if r[i] != w:
		if mat[i][r[i]] == 1:
			z = 0
			break
for i in range(w):
	if c[i] != h:
		if mat[c[i]][i] == 1:
			z = 0
			break
if z == 0:
	print(0)
else:
	ans = 0
	for i in range(1, h):
		for j in range(1, w):
			if mat[i][j] == 0:
				if mat[i - 1][j] == 0 and mat[i][j - 1] == 0:
					ans += 1
				elif j > r[i] and i > c[j]:
					ans += 1
	print(pow(2, ans, 10 ** 9 + 7))


(h, w) = list(map(int, input().split(' ')))
r = list(map(int, input().split(' ')))
c = list(map(int, input().split(' ')))
counter = 0
flag = False
for i in range(h):
	for j in range(w):
		if i > c[j] and j > r[i]:
			counter += 1
		if i == c[j] and j < r[i] or (i < c[j] and j == r[i]):
			flag = True
if counter > 0 and (not flag):
	print(2 ** counter % 1000000007)
elif flag:
	print('0')
else:
	print('1')


def validaValores(a1, a2):
	for i in range(len(a1)):
		valor = a1[i]
		if valor < len(a2):
			vcol = a2[valor]
			if i + 1 <= vcol and vcol >= valor:
				return False
	for j in range(len(a2)):
		valor = a2[j]
		if valor < len(a1):
			vLin = a1[valor]
			if j + 1 <= vLin and vLin >= valor:
				return False
	return True
(r, c) = [*map(int, input().split())]
linhas = [*map(int, input().split())]
colunas = [*map(int, input().split())]
mod = 1000000007
total = r * c
disponiveis = 0
invalido = not validaValores(linhas, colunas)
if invalido:
	ans = 0
else:
	for i in range(r):
		for j in range(c):
			vl = linhas[i]
			vc = colunas[j]
			if not ((vl == 0 or vc == 0) and (i == 0 or j == 0)) and (not vl >= j) and (not vc >= i):
				disponiveis += 1
	ans = 2 ** disponiveis % mod
print(ans)


(h, w) = (int(i) for i in input().split())
r = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]
d = [[2 for i in range(w)] for j in range(h)]
for i in range(h):
	if r[i] == 0:
		d[i][0] = 0
	else:
		for j in range(r[i]):
			d[i][j] = 1
			if j < w - 1:
				d[i][j + 1] = 0
for i in range(w):
	if c[i] == 0:
		d[0][i] = 0
	else:
		for j in range(c[i]):
			d[j][i] = 1
			if j < h - 1:
				d[j + 1][i] = 0
r2 = [0 for i in range(h)]
for i in range(h):
	for j in range(w):
		if d[i][j] == 0:
			break
		else:
			r2[i] += 1
c2 = [0 for i in range(w)]
for j in range(w):
	for i in range(h):
		if d[i][j] == 0:
			break
		else:
			c2[j] += 1
for i in range(h):
	if r[i] != r2[i]:
		print(0)
		exit()
for i in range(w):
	if c[i] != c2[i]:
		print(0)
		exit()
ans = 1
for i in range(0, h):
	for j in range(0, w):
		if d[i][j] == 2:
			ans *= 2
			ans %= 10 ** 9 + 7
print(ans)


hw = [int(i) for i in input().split()]
h = hw[0]
w = hw[1]
r = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]
total = 0
ans = 1
for i in range(w):
	for j in range(c[i]):
		if r[j] == i:
			print(0)
			quit()
for i in range(h):
	for j in range(r[i]):
		if c[j] == i:
			print(0)
			flag = True
			quit()
for i in range(h):
	for j in range(w):
		if j > r[i] and i > c[j]:
			total += 1
for i in range(total):
	ans = ans * 2 % 1000000007
print(ans)


def paint():
	for y in range(len(r_arr)):
		r = r_arr[y]
		for x in range(r):
			arr[y][x] = 1
		if r < w:
			arr[y][r] = 0
	for x in range(len(c_arr)):
		c = c_arr[x]
		for y in range(c):
			if arr[y][x] == 0:
				return 0
			else:
				arr[y][x] = 1
		if c < h:
			if arr[c][x] == 1:
				return 0
			else:
				arr[c][x] = 0
	return 1
(h, w) = map(int, input().split())
r_arr = list(map(int, input().split()))
c_arr = list(map(int, input().split()))
arr = [[2 for _ in range(w)] for _ in range(h)]
pos = paint()
if not pos:
	print(0)
else:
	cnt = 0
	for y in range(h):
		for x in range(w):
			if arr[y][x] == 2:
				cnt += 1
	print(2 ** cnt % 1000000007)


MOD = 1000000007
(h, w) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
m = [['x' for i in range(w)] for j in range(h)]
for j in range(w):
	black = c[j]
	if black == 0:
		m[0][j] = 'w'
		continue
	for i in range(black):
		m[i][j] = 'b'
	if black < h:
		m[black][j] = 'w'
buggy = False
for i in range(h):
	if buggy:
		break
	black = r[i]
	if black == 0:
		if m[i][0] == 'b':
			buggy = True
			break
		m[i][0] = 'w'
		continue
	for j in range(black):
		if m[i][j] == 'w':
			buggy = True
			break
		m[i][j] = 'b'
	if black < w:
		if buggy or m[i][black] == 'b':
			buggy = True
			break
		m[i][black] = 'w'
if not buggy:
	count = 0
	for i in range(h):
		for j in range(w):
			if m[i][j] == 'x':
				count += 1
	print(pow(2, count, MOD))
else:
	print(0)


def main():
	MOD = 10 ** 9 + 7
	(H, W) = (int(i) for i in input().split())
	r = [int(i) for i in input().split()]
	c = [int(i) for i in input().split()]
	seen = [[-1 for i in range(W)] for j in range(H)]
	for i in range(H):
		for j in range(min(r[i] + 1, W)):
			if j <= r[i] - 1:
				if seen[i][j] == -1:
					seen[i][j] = 1
				elif seen[i][j] == 0:
					seen[i][j] = -2
			elif seen[i][j] == -1:
				seen[i][j] = 0
			elif seen[i][j] == 1:
				seen[i][j] = -2
	for j in range(W):
		for i in range(min(c[j] + 1, H)):
			if i <= c[j] - 1:
				if seen[i][j] == -1:
					seen[i][j] = 1
				elif seen[i][j] == 0:
					seen[i][j] = -2
			elif seen[i][j] == -1:
				seen[i][j] = 0
			elif seen[i][j] == 1:
				seen[i][j] = -2
	ans = 1
	flag = False
	for i in range(H):
		for j in range(W):
			if seen[i][j] == -1:
				ans = ans * 2 % MOD
			elif seen[i][j] == -2:
				flag = True
		if flag:
			print(0)
			break
	else:
		print(ans % MOD)
main()


(h, w) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
grid = [[0 for i in range(w)] for i in range(h)]
flag = 1
for i in range(w):
	for j in range(c[i]):
		grid[j][i] = 1
	if c[i] < h:
		grid[c[i]][i] = 2
for i in range(h):
	for j in range(r[i]):
		if grid[i][j] == 2:
			flag = 0
		grid[i][j] = 1
	if r[i] < w:
		if grid[i][r[i]] == 1:
			flag = 0
		grid[i][r[i]] = 2
c = 0
if flag:
	for i in grid:
		c += i.count(0)
	print(pow(2, c, 1000000007))
else:
	print(0)


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)
threading.stack_size(2 ** 27)
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):
	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):
	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:
			x += b
		b = b // 2
	return x
TestCases = False
optimise_for_recursion = True

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
	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(0)
				return
			mat[ele][i] = -1
	res = 1
	for li in mat:
		for i in li:
			if i == 0:
				res = res * 2 % P
	print(res)
if TestCases:
	for i in range(get_int()):
		main()
else:
	main() if not optimise_for_recursion else threading.Thread(target=main).start()


import sys
from collections import defaultdict as dd
mod = 10 ** 9 + 7

def ri(flag=0):
	if flag == 0:
		return [int(i) for i in sys.stdin.readline().split()]
	else:
		return int(sys.stdin.readline())
(n, m) = ri()
a = ri()
b = ri()
right = [[0 for i in range(m + 1)] for j in range(n + 1)]
mat = [[0 for i in range(m)] for j in range(n)]
for i in range(n):
	take = a[i]
	for j in range(take):
		mat[i][j] = 1
for i in range(m):
	take = b[i]
	for j in range(take):
		mat[j][i] = 1
newa = [0 for i in range(n)]
newb = [0 for i in range(m)]
for i in range(n):
	cur = 0
	for j in range(m):
		if mat[i][j] == 1:
			cur += 1
		else:
			break
	newa[i] = cur
for i in range(m):
	cur = 0
	for j in range(n):
		if mat[j][i] == 1:
			cur += 1
		else:
			break
	newb[i] = cur
if newa != a or newb != b:
	print(0)
else:
	ans = 1
	f = 1
	for i in range(n):
		for j in range(m):
			if a[i] + 1 > j or b[j] + 1 > i:
				pass
			else:
				f = 0
				ans = ans * 2
				ans %= mod
	print(ans)


import sys
import itertools
import math
import collections
from collections import Counter

def getdict(n):
	d = {}
	if type(n) is list:
		for i in n:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
	else:
		for i in range(n):
			t = ii()
			if t in d:
				d[t] += 1
			else:
				d[t] = 1
	return d

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

def divs(n, start=1):
	r = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				r.append(i)
			else:
				r.extend([i, n // i])
	return r

def cdiv(n, k):
	return n // k + (n % k != 0)

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(map(int, input().split()))

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def prr(a, sep=' '):
	print(sep.join(map(str, a)))

def dd():
	return collections.defaultdict(int)
(n, m) = mi()
d = [[-1] * m for i in range(n)]
r = li()
c = li()
for i in range(n):
	for j in range(r[i]):
		d[i][j] = 1
	if r[i] != m:
		d[i][r[i]] = 0
for i in range(m):
	for j in range(c[i]):
		if d[j][i] == 0:
			exit(print(0))
		d[j][i] = 1
	if c[i] != n:
		if d[c[i]][i] == 1:
			exit(print(0))
		d[c[i]][i] = 0
res = 1
for i in range(n):
	t = d[i].count(-1)
	res = res * 2 ** t % (10 ** 9 + 7)
print(res % (10 ** 9 + 7))


mod = 1000000007
maxn = 1010

def mypow(a, b):
	global mod
	ans = 1
	while b:
		if b & 1:
			ans = ans * a % mod
		a = a * a % mod
		b >>= 1
	return ans

def read(x, n):
	s = input().split()
	for i in range(n):
		x[i] = int(s[i])

def main():
	global maxn
	a = [[0 for x in range(maxn)] for y in range(maxn)]
	x = [0 for i in range(maxn)]
	y = [0 for i in range(maxn)]
	s = input().split()
	n = int(s[0])
	m = int(s[1])
	read(x, n)
	read(y, m)
	ans = 2
	for i in range(n):
		for j in range(x[i]):
			a[i][j] = 1
		a[i][x[i]] = 2
	for i in range(m):
		for j in range(y[i]):
			if a[j][i] == 2:
				ans = 0
			else:
				a[j][i] = 1
		if a[y[i]][i] == 1:
			ans = 0
		else:
			a[y[i]][i] = 2
	cnt = 0
	for i in range(n):
		for j in range(m):
			if not a[i][j]:
				cnt += 1
	if not ans:
		print(ans)
	else:
		print(mypow(ans, cnt))
main()


(n, m) = map(int, input().split())
mas = [[0] * m for i in range(n)]
r = list(map(int, input().split()))
for i in range(n):
	for j in range(r[i]):
		mas[i][j] = 1
c = list(map(int, input().split()))
for i in range(m):
	for j in range(c[i]):
		mas[j][i] = 1
for i in range(n):
	s = 0
	j = 0
	while j < m and mas[i][j] == 1:
		s += 1
		j += 1
	if s != r[i]:
		print(0)
		exit()
for i in range(m):
	s = 0
	j = 0
	while j < n and mas[j][i] == 1:
		s += 1
		j += 1
	if s != c[i]:
		print(0)
		exit()
ans = 1
for i in range(n):
	for j in range(r[i] + 1, m):
		if c[j] < i:
			ans = ans * 2 % 1000000007
print(ans)


(h, w) = map(int, input().split())
hv = list(map(int, input().split()))
wv = list(map(int, input().split()))
(hv, wv) = (wv, hv)
a = [[-1] * w for i in range(h)]
for i in range(w):
	for j in range(hv[i]):
		if abs(a[j][i]) == 1:
			a[j][i] = 1
		else:
			print(0)
			exit(0)
	try:
		if a[hv[i]][i] < 1:
			a[hv[i]][i] = 0
		else:
			print(0)
			exit(0)
	except IndexError:
		pass
for i in range(h):
	for j in range(wv[i]):
		if abs(a[i][j]) == 1:
			a[i][j] = 1
		else:
			print(0)
			exit(0)
	try:
		if a[i][wv[i]] < 1:
			a[i][wv[i]] = 0
		else:
			print(0)
			exit(0)
	except IndexError:
		pass
c = 1
for i in range(h):
	for j in range(w):
		if a[i][j] == -1:
			c *= 2
			c %= 1000000007
print(c)


inp = input()
w = int(inp.split(' ')[1])
h = int(inp.split(' ')[0])
s = input()
r = [int(s.split(' ')[i]) for i in range(h)]
s = input()
c = [int(s.split(' ')[i]) for i in range(w)]
mem = [[0 for i in range(w + 1)] for j in range(h + 1)]
for i in range(h):
	for j in range(r[i]):
		if mem[i][j] == -1:
			print(0)
			exit()
		mem[i][j] = 1
	if mem[i][r[i]] == 1:
		print(0)
		exit()
	mem[i][r[i]] = -1
for i in range(w):
	for j in range(c[i]):
		if mem[j][i] == -1:
			print(0)
			exit()
		mem[j][i] = 1
	if mem[c[i]][i] == 1:
		print(0)
		exit()
	mem[c[i]][i] = -1
z = sum((x == 0 for row in mem[:-1] for x in row[:-1]))
print(pow(2, z) % 1000000007)


def power(d, count, m):
	if count == 0:
		return 1
	elif count % 2 == 0:
		return power(d * d % m, count // 2, m)
	else:
		return d * power(d * d % m, count // 2, m) % m
(h, w) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
grid = [[0 for i in range(w + 1)] for j in range(h + 1)]
c = 0
for row in range(h):
	for i in range(a[row] + 1):
		if i == a[row]:
			if grid[row][i] == 0 or grid[row][i] == 'o':
				grid[row][i] = 'o'
			else:
				c = 1
				break
		elif grid[row][i] == 0 or grid[row][i] == 'c':
			grid[row][i] = 'c'
		else:
			c = 1
			break
	if c == 1:
		break
for col in range(w):
	for i in range(b[col] + 1):
		if i == b[col]:
			if grid[i][col] == 0 or grid[i][col] == 'o':
				grid[i][col] = 'o'
			else:
				c = 1
				break
		elif grid[i][col] == 0 or grid[i][col] == 'c':
			grid[i][col] = 'c'
		else:
			c = 1
			break
	if c == 1:
		break
if c != 1:
	count = 0
	for row in range(h):
		for col in range(w):
			if grid[row][col] == 0:
				count += 1
	ans = power(2, count, 1000000007)
	print(ans)
else:
	print(0)


import sys

def rint():
	return map(int, sys.stdin.readline().split())
(h, w) = rint()
r = list(rint())
c = list(rint())
mat = [[-1 for i in range(w)] for j in range(h)]
for i in range(h):
	cf = r[i]
	for j in range(0, cf):
		mat[i][j] = 1
	if cf < w:
		mat[i][cf] = 0
for i in range(w):
	cf = c[i]
	for j in range(0, cf):
		if mat[j][i] == 0:
			print(0)
			exit()
		mat[j][i] = 1
	if cf < h:
		if mat[cf][i] == 1:
			print(0)
			exit()
		mat[cf][i] = 0
cnt = 0
for i in range(h):
	cnt += mat[i].count(-1)
ans = 1
for i in range(cnt):
	ans *= 2
	ans = ans % (10 ** 9 + 7)
print(ans)


g = [[False] * 1003 for x in range(1003)]
[h, w] = [int(x) for x in input().split()]
rows = [int(x) for x in input().split()]
cols = [int(x) for x in input().split()]
for r in range(0, h):
	for c in range(rows[r]):
		g[c][r] = True
for c in range(0, w):
	for r in range(cols[c]):
		g[c][r] = True
for r in range(0, h):
	if rows[r] != w and g[rows[r]][r] == True:
		print(0)
		exit()
for c in range(0, w):
	if cols[c] != w and g[c][cols[c]] == True:
		print(0)
		exit()
ans = 1
for c in range(w):
	for r in range(h):
		if g[c][r] == False and c != rows[r] and (r != cols[c]):
			ans *= 2
			ans %= 10 ** 9 + 7
print(ans)


inps = [int(a) for a in input().split()]
(h, w) = (inps[0], inps[1])
ctr = 0
ans = 0
rs = [int(a) for a in input().split()][:h]
cs = [int(a) for a in input().split()][:w]
ar = list()
for i in range(h):
	for j in range(w):
		if rs[i] < j and cs[j] < i:
			ctr += 1
		if rs[i] >= j and cs[j] >= i:
			if rs[i] == j and cs[j] > i:
				ans = -1
			elif rs[i] > j and cs[j] == i:
				ans = -1
if ans == -1:
	print(0)
else:
	print(2 ** ctr % 1000000007)


a = list(map(int, input().split()))
r = a[0]
c = a[1]
ri = list(map(int, input().split()))
ci = list(map(int, input().split()))
arr = [['_'] * c for i in range(r)]
for i in range(r):
	itr = ri[i]
	for j in range(itr):
		arr[i][j] = 1
	if itr < c:
		arr[i][itr] = 0
kk = 1
for j in range(c):
	itr = ci[j]
	for i in range(itr):
		if arr[i][j] == '_' or arr[i][j] == 1:
			arr[i][j] = 1
		else:
			kk = 0
			break
	if itr < r:
		if arr[itr][j] == '_' or arr[itr][j] == 0:
			arr[itr][j] = 0
		else:
			kk = 0
			break
	if kk == 0:
		break
if kk == 0:
	print(0)
else:
	count = 0
	for i in range(r):
		count += arr[i].count('_')
	print(2 ** count % (10 ** 9 + 7))


(H, W) = (int(x) for x in input().split())
M = [[False for _w in range(W)] for _h in range(H)]
Paint = [[False for _w in range(W)] for _h in range(H)]
can_create = True
for r in enumerate((int(x) for x in input().split())):
	for i in range(min(r[1], W)):
		if M[r[0]][i] and (not Paint[r[0]][i]):
			can_create = False
		M[r[0]][i] = True
		Paint[r[0]][i] = True
	if r[1] < W:
		if M[r[0]][r[1]] and Paint[r[0]][r[1]]:
			can_create = False
		M[r[0]][r[1]] = True
		Paint[r[0]][r[1]] = False
for c in enumerate((int(x) for x in input().split())):
	for i in range(min(c[1], H)):
		if M[i][c[0]] and (not Paint[i][c[0]]):
			can_create = False
		M[i][c[0]] = True
		Paint[i][c[0]] = True
	if c[1] < H:
		if M[c[1]][c[0]] and Paint[c[1]][c[0]]:
			can_create = False
		M[c[1]][c[0]] = True
		Paint[c[1]][c[0]] = False
if not can_create:
	print(0)
	exit()
cnt = 0
for m in M:
	for n in m:
		if not n:
			cnt += 1
ans = 1
for _ in range(cnt):
	ans = ans * 2 % 1000000007
print(ans)


(h, w) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
l = []
count = h * w
for i in range(h):
	l.append([0] * w)
flag = True
for i in range(h):
	if r[i] < w and c[r[i]] > i:
		flag = False
		break
	for j in range(w):
		if c[j] < h and r[c[j]] > j:
			flag = False
			break
		if j <= r[i] or i <= c[j]:
			l[i][j] = 1
			count -= 1
	if flag == False:
		break
if flag == False:
	print(0)
else:
	print(pow(2, count, 1000000007))


R = lambda : map(int, input().split())

def f(L):
	for i in range(n):
		s = 0
		for j in range(m):
			if L[i][j] == 1:
				s += 1
			else:
				break
		if s != a[i]:
			return 1
	for i in range(m):
		s = 0
		for j in range(n):
			if L[j][i] == 1:
				s += 1
			else:
				break
		if s != b[i]:
			return 1
(n, m) = R()
a = list(R())
b = list(R())
L = [[0 for i in range(m)] for j in range(n)]
for (ind, i) in enumerate(a):
	for j in range(i):
		L[ind][j] = 1
for (ind, i) in enumerate(b):
	for j in range(i):
		L[j][ind] = 1
if f(L):
	print(0)
else:
	c = 0
	for i in range(n):
		for j in range(m):
			if j - a[i] > 0 and i - b[j] > 0:
				c += 1
	M = 10 ** 9 + 7
	print(pow(2, c, M))


(h, w) = map(int, input().split())
r = [int(j) for j in input().split()]
c = [int(j) for j in input().split()]
arr = [[0 for j in range(w)] for j in range(h)]
temp = [[0 for j in range(w)] for j in range(h)]
flag = False
for i in range(h):
	for j in range(r[i]):
		temp[i][j] = 1
	try:
		temp[i][r[i]] = 2
	except IndexError:
		pass
for i in range(w):
	for j in range(c[i]):
		if temp[j][i] == 2:
			flag = True
		temp[j][i] = 1
	try:
		if temp[c[i]][i] == 1:
			flag = True
	except IndexError:
		pass
if flag == True:
	print(0)
	exit()
for i in range(h):
	for j in range(w):
		try:
			if r[i] >= j:
				arr[i][j] = 1
		except IndexError:
			pass
for i in range(w):
	for j in range(h):
		try:
			if c[i] >= j:
				arr[j][i] = 1
		except IndexError:
			pass
ans = 1
for x in arr:
	for y in x:
		if y == 0:
			ans *= 2
			ans %= 10 ** 9 + 7
print(ans)


_ = input()
r = list(map(int, input().split(' ')))
c = list(map(int, input().split(' ')))
arr = [[0] * len(c) for _ in range(len(r))]

def solve():
	for (i, x) in enumerate(r):
		for j in range(0, x):
			arr[i][j] = 1
		if x < len(c):
			arr[i][x] = -1
	for (i, x) in enumerate(c):
		for j in range(0, x):
			if arr[j][i] == -1:
				return 0
			arr[j][i] = 1
		if x < len(r):
			if arr[x][i] == 1:
				return 0
			arr[x][i] = -1
	s = 0
	for row in arr:
		for x in row:
			s += x == 0
	return (1 << s) % 1000000007
print(solve())


(h, w) = list(map(int, input().split()))
r = list(map(int, input().split()))
c = list(map(int, input().split()))
temp = [[-1 for i in range(w + 1)] for j in range(h + 1)]
res = 0
for i in range(h):
	for j in range(r[i]):
		temp[i][j] = 1
	temp[i][r[i]] = 0
check = False
for i in range(w):
	for j in range(c[i]):
		if temp[j][i] == 0:
			check = True
			break
		temp[j][i] = 1
	if temp[c[i]][i] == 1:
		check = True
		break
	else:
		temp[c[i]][i] = 0
if check:
	print('0')
else:
	for i in range(1, h):
		for j in range(1, w):
			if temp[i][j] == -1:
				res += 1
	print(2 ** res % (10 ** 9 + 7))


from sys import stdin
A = list(map(int, stdin.readline().split()))
B = list(map(int, stdin.readline().split()))
C = list(map(int, stdin.readline().split()))
D = [[0 for x in range(A[1])] for y in range(A[0])]
sum = 0
K = 0
for u in range(0, A[0]):
	if B[u] > A[1]:
		print(0)
		K = 1
		break
	if B[u] == 0:
		D[u][0] = 'W'
		sum += 1
	for t in range(0, B[u]):
		D[u][t] = 'B'
		sum += 1
		if t == B[u] - 1 and t < A[1] - 1:
			D[u][t + 1] = 'W'
			sum += 1
if K == 0:
	for w in range(0, A[1]):
		if C[w] > A[0]:
			print(0)
			K = 1
			break
		if C[w] == 0:
			if D[0][w] == 'B':
				print(0)
				K = 1
				break
			elif D[0][w] == 0:
				D[0][w] = 'W'
				sum += 1
				continue
		for r in range(0, C[w]):
			if D[r][w] == 'W':
				print(0)
				K = 1
				break
			elif D[r][w] == 0:
				D[r][w] = 'B'
				sum += 1
			if r == C[w] - 1 and r < A[0] - 1:
				if D[r + 1][w] == 'B':
					print(0)
					K = 1
					break
				elif D[r + 1][w] == 0:
					D[r + 1][w] = 'W'
					sum += 1
		if K == 1:
			break
QQ = A[0] * A[1] - sum
begin = 1
if K == 0:
	while QQ > 0:
		begin *= 2
		QQ -= 1
		begin = begin % (10 ** 9 + 7)
	print(begin)


(h, w) = map(int, input().split())
ha = list(map(int, input().split()))
wa = list(map(int, input().split()))
f = 0
for i in range(h):
	for j in range(w):
		bl = j < ha[i] or i < wa[j]
		wh = j == ha[i] or i == wa[j]
		if bl and wh:
			print(0)
			exit()
		if not bl and (not wh):
			f += 1
ans = 1
for i in range(f):
	ans *= 2
	ans %= 1000000007
print(ans)


def solve(h, w, r, c):
	a = []
	m = []
	for v in range(h):
		a.append([-1] * w)
	for v in range(h):
		m.append([-1] * w)
	for v in range(h):
		for x in range(r[v]):
			a[v][x] -= 1
		if r[v] < w:
			a[v][r[v]] += 1
		for x in range(r[v] + 1, w):
			m[v][x] += 1
	for v in range(w):
		for x in range(c[v]):
			if a[x][v] == 0:
				return 0
		if c[v] < h and a[c[v]][v] == -2:
			return 0
		for x in range(c[v] + 1, h):
			m[x][v] += 1
	result = 0
	for v in m:
		result += len([x for x in v if x == 1])
	return 1 if result == 0 else pow(2, result) % 1000000007
(h, w) = map(int, input().split())
r = list(map(int, input().split()))
c = list(map(int, input().split()))
print(solve(h, w, r, c))


(n, m) = list(map(int, input().split()))
r = list(map(int, input().split()))
c = list(map(int, input().split()))
a = [[0 for j in range(m)] for i in range(n)]
for i in range(n):
	for j in range(r[i]):
		a[i][j] = 1
for j in range(m):
	for i in range(c[j]):
		a[i][j] = 1
p = set()
for i in range(n):
	if r[i] < m:
		p.add((i, r[i]))
		if a[i][r[i]]:
			print(0)
			exit()
for j in range(m):
	if c[j] < n:
		p.add((c[j], j))
		if a[c[j]][j]:
			print(0)
			exit()
k = 0
for i in range(n):
	for j in range(m):
		if not a[i][j]:
			k += 1
print(2 ** (k - len(p)) % 1000000007)


(h, w) = list(map(int, input().split()))
row = list(map(int, input().split()))
col = list(map(int, input().split()))
pos = 0
valid = 1
for i in range(len(row)):
	loop = 0
	for j in range(0, row[i]):
		loop += 1
		if col[j] == i:
			valid = 0
			break
	if valid == 1 and loop < len(col):
		if col[loop] > i:
			valid = 0
if valid == 1:
	for i in range(len(row)):
		for j in range(row[i] + 1, len(col)):
			if col[j] < i:
				pos += 1
	ans = 1
	m = 10 ** 9 + 7
	for j in range(pos):
		ans = ans % m * (2 % m) % m
	print(ans)
else:
	print(0)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

def go():
	c = 0
	for i in range(n):
		for j in range(m):
			if j > a[i] and i > b[j]:
				c += 1
			elif j < a[i] and i == b[j] or (j == a[i] and i < b[j]):
				print(0)
				return
	print(pow(2, c, 1000000007))
go()


def validar_valores(a1, a2):
	for i in range(len(a1)):
		valor = a1[i]
		if valor < len(a2):
			vcol = a2[valor]
			if i + 1 <= vcol and vcol >= valor:
				return False
	for j in range(len(a2)):
		valor = a2[j]
		if valor < len(a1):
			vLin = a1[valor]
			if j + 1 <= vLin and vLin >= valor:
				return False
	return True
(rows, cols) = [int(x) for x in input().split()]
linhas = [int(x) for x in input().split()]
value_cols = [int(x) for x in input().split()]
modulo = 1000000007
total = rows * cols
disponiveis = 0
invalido = not validar_valores(linhas, value_cols)
if invalido:
	resp = 0
else:
	for i in range(rows):
		for j in range(cols):
			vl = linhas[i]
			vc = value_cols[j]
			if not ((vl == 0 or vc == 0) and (i == 0 or j == 0)) and (not vl >= j) and (not vc >= i):
				disponiveis += 1
	resp = pow(2, disponiveis) % modulo
print(resp)


(h, w) = map(int, input().split())
l1 = list(map(int, input().split()))
l2 = list(map(int, input().split()))
a = True
for i in range(h):
	if l1[i] < w:
		if l2[l1[i]] > i:
			a = False
for i in range(w):
	if l2[i] < h:
		if l1[l2[i]] > i:
			a = False
if a == False:
	print(0)
else:
	ans = 1
	for i in range(1, h):
		for j in range(l1[i] + 1, w):
			if l2[j] < i:
				ans *= 2
				ans %= 10 ** 9 + 7
	print(ans % 1000000007)