#include <bits/stdc++.h>
using namespace std;
const int INF = 2147483647;
const int INF2 = 0x3f3f3f3f;
const long long INF64 = 1e18;
const double INFD = 1e30;
const double EPS = 1e-6;
const double PI = 3.14159265;
const long long MOD = 1e9 + 7;
template <typename T>
inline T read() {
  T X = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
  return w ? -X : X;
}
const int MAXN = 1005;
int n, m, k;
int arr[MAXN];
int _map[MAXN][MAXN];
int rr[MAXN];
int cc[MAXN];
int quickM(long long x, long long p, int mod) {
  if (mod == 0) return 1;
  x %= mod;
  long long ans = 1;
  while (p) {
    if (p & 1) ans = ans * x % mod;
    x = x * x % mod;
    p >>= 1;
  }
  return ans;
}
bool check(int i, int j, int c) {
  if (_map[i][j] == 3 - c) {
    printf("0");
    exit(0);
  }
  _map[i][j] = c;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    rr[i] = read<int>();
  }
  for (int i = 1; i <= m; i++) {
    cc[i] = read<int>();
  }
  for (int i = 1; i <= n; i++) {
    if (rr[i] == 0) {
      check(i, 1, 1);
      continue;
    }
    for (int j = 1; j <= rr[i]; j++) {
      check(i, j, 2);
    }
    check(i, rr[i] + 1, 1);
  }
  for (int i = 1; i <= m; i++) {
    if (cc[i] == 0) {
      check(1, i, 1);
      continue;
    }
    for (int j = 1; j <= cc[i]; j++) {
      check(j, i, 2);
    }
    check(cc[i] + 1, i, 1);
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (!_map[i][j]) cnt++;
    }
  }
  printf("%d\n", quickM(2, cnt, MOD));
  return 0;
}
