#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
long long gcd(long long x, long long y) {
  if (y == 0)
    return x;
  else
    return gcd(y, x % y);
}
long long ksm(long long a, long long b) {
  long long r = 1;
  while (b) {
    if (b & 1) r = (r * a) % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return r;
}
void exgcd(long long a, long long b, long long& d, long long& x, long long& y) {
  if (!b) {
    d = a;
    x = 1;
    y = 0;
  } else {
    exgcd(b, a % b, d, y, x);
    y -= x * (a / b);
  }
}
long long inv(long long a, long long p) {
  long long d, x, y;
  exgcd(a, p, d, x, y);
  return d == 1 ? (x + p) % p : -1;
}
inline int read() {
  char c = getchar();
  int x = 0, f = 1;
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}
const int maxn = 1e5 + 100;
int n, m, k, t, a[1005][1005];
int r[1005], c[1005];
int main() {
  int x, y;
  cin >> x >> y;
  for (int i = 1; i <= x; i++) cin >> r[i];
  for (int i = 1; i <= y; i++) cin >> c[i];
  memset(a, 0, sizeof(a));
  for (int i = 1; i <= x; i++) {
    if (r[i] == 0)
      a[i][1] = -1;
    else {
      for (int j = 1; j <= r[i]; j++) a[i][j] = 1;
      a[i][r[i] + 1] = -1;
    }
  }
  int flag = 1;
  for (int i = 1; i <= y; i++) {
    if (c[i] == 0) {
      if (a[1][i] == 1) flag = 0;
      a[1][i] = -1;
    } else {
      for (int j = 1; j <= c[i]; j++) {
        if (a[j][i] == -1) flag = 0;
        a[j][i] = 1;
      }
      if (a[c[i] + 1][i] == 1) flag = 0;
      a[c[i] + 1][i] = -1;
    }
  }
  if (!flag) {
    cout << "0\n";
    return 0;
  }
  long long ans = 1;
  for (int i = 1; i <= x; i++)
    for (int j = 1; j <= y; j++) {
      if (a[i][j] == 0) ans = ans * 2ll % mod;
    }
  cout << ans << endl;
  return 0;
}
