#include <bits/stdc++.h>
using namespace std;
constexpr int INF = 0x3f3f3f3f;
const int maxn = 1005;
int f[maxn][maxn];
int r[maxn];
int c[maxn];
const int BLACK = -1;
const int WHITE = 1;
const long long mod = 1e9 + 7;
long long qpow_mod(long long a, long long b) {
  long long rtn = 1;
  for (; b; b >>= 1) {
    if (b & 1) rtn = rtn * a % mod;
    a = a * a % mod;
  }
  return rtn;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d", &r[i]);
  }
  for (int i = 0; i < m; i++) {
    scanf("%d", &c[i]);
  }
  int tot = n * m;
  bool flag = true;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < r[i]; j++) {
      if (!f[i][j]) {
        f[i][j] = BLACK;
      }
    }
    if (r[i] < m) {
      if (!f[i][r[i]])
        f[i][r[i]] = WHITE;
      else {
        flag = false;
        break;
      }
    } else
      tot++;
    tot -= r[i] + 1;
  }
  for (int j = 0; j < m; j++) {
    for (int i = 0; i < c[j]; i++) {
      if (!f[i][j]) {
        f[i][j] = BLACK;
      } else if (f[i][j] == BLACK) {
        tot++;
      } else {
        flag = false;
        break;
      }
    }
    if (c[j] < n) {
      if (!f[c[j]][j]) {
        f[c[j]][j] = WHITE;
      } else if (f[c[j]][j] == WHITE) {
        tot++;
      } else {
        flag = false;
        break;
      }
    } else
      tot++;
    tot -= c[j] + 1;
  }
  if (flag) {
    printf("%lld", qpow_mod(2, tot));
  } else {
    printf("0");
  }
  return 0;
}
