#include <bits/stdc++.h>
using namespace std;
namespace fast {
inline char nc() {
  static char buf[100000], *L = buf, *R = buf;
  return L == R && (R = (L = buf) + fread(buf, 1, 100000, stdin), L == R)
             ? EOF
             : *L++;
}
template <class orz>
inline void qread(orz &x) {
  x = 0;
  char ch = nc();
  bool f = 0;
  while (ch < '0' || ch > '9') (ch == '-') && (f = 1), ch = nc();
  while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + (ch ^ 48), ch = nc();
  f && (x = -x);
}
}  // namespace fast
using namespace fast;
template <class orz>
inline void read(orz &x) {
  x = 0;
  bool f = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') (ch == '-') && (f = 1), ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
  f && (x = -x);
}
template <class orz>
inline void out(orz x) {
  (x < 0) && (putchar('-'), x = -x);
  if (x > 9) out(x / 10);
  putchar(x % 10 + '0');
}
const double PI = acos(-1);
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
const long long mod = 1e9 + 7;
const int maxn = 1e3 + 5;
int a[maxn][maxn];
int n, m;
int r[maxn], c[maxn];
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) read(r[i]);
  for (int i = 0; i < m; i++) read(c[i]);
  for (int i = 0; i < n; i++) {
    if (r[i] == 0) {
      a[i][0] = -1;
    } else {
      for (int j = 0; j < r[i]; j++) {
        a[i][j] = 1;
      }
      a[i][r[i]] = -1;
    }
  }
  int flag = 0;
  long long sum = 1;
  for (int i = 0; i < m; i++) {
    if (c[i] == 0) {
      if (a[0][i] == 0 || a[0][i] == -1)
        a[0][i] = -1;
      else {
        flag = 1;
        break;
      }
    } else {
      for (int j = 0; j < c[i]; j++) {
        if (a[j][i] == 0 || a[j][i] == 1)
          a[j][i] = 1;
        else {
          flag = 1;
          break;
        }
      }
      if (a[c[i]][i] == 0 || a[c[i]][i] == -1)
        a[c[i]][i] = -1;
      else {
        flag = 1;
        break;
      }
    }
  }
  if (!flag) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (a[i][j] == 0) sum = sum * 2 % mod;
      }
    }
    cout << sum << endl;
  } else
    cout << "0" << endl;
  return 0;
}
