#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
const long long inf = 1e9 + 7;
long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
  return f * x;
}
int n, m, a[1005], b[1005], s;
bool c[1005][1005];
int main() {
  n = read();
  m = read();
  for (int i = 1; i <= n; i++) a[i] = read();
  for (int i = 1; i <= m; i++) b[i] = read();
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= a[i]; j++) c[i][j] = true;
  for (int j = 1; j <= m; j++)
    for (int i = 1; i <= b[j]; i++) c[i][j] = true;
  for (int i = 1; i <= n; i++)
    if (c[i][a[i] + 1]) return 0 * printf("0\n");
  for (int j = 1; j <= m; j++)
    if (c[b[j] + 1][j]) return 0 * printf("0\n");
  for (int i = 1; i <= n; i++) c[i][a[i] + 1]++;
  for (int j = 1; j <= m; j++) c[b[j] + 1][j]++;
  s = 1;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (!c[i][j]) s = s * 2 % inf, c[i][j]++;
  printf("%d\n", s);
  return 0;
}
