#include <bits/stdc++.h>
const int INF = 1 << 29;
int cmp(const void* a, const void* b);
long long maxv(long long a, long long b);
long long minv(long long a, long long b);
long long gcd(long long u, long long v);
long long digits(long long n);
bool isPoweroftwo(long long n);
int main() {
  long long h, w;
  scanf("%lld%lld", &h, &w);
  long long mat[h][w];
  for (long long i = 0; i < (h); i++) {
    for (long long j = 0; j < (w); j++) {
      mat[i][j] = -1;
    }
  }
  for (long long i = 0; i < (h); i++) {
    long long r;
    scanf("%lld", &r);
    for (long long j = 0; j < (r); j++) mat[i][j] = 1;
    if (r < w) mat[i][r] = 0;
  }
  long long f = 0;
  for (long long i = 0; i < (w); i++) {
    long long r;
    scanf("%lld", &r);
    for (long long j = 0; j < (r); j++) {
      if (mat[j][i] == 0) {
        f = 1;
        break;
      }
      mat[j][i] = 1;
    }
    if (r < h) {
      if (mat[r][i] == 1) {
        f = 1;
        break;
      }
      mat[r][i] = 0;
    }
  }
  if (f)
    printf("0\n");
  else {
    long long c = 0;
    for (long long i = 0; i < (h); i++) {
      for (long long j = 0; j < (w); j++) {
        if (mat[i][j] == -1) c++;
      }
    }
    long long ans = 1;
    for (long long i = 0; i < (c); i++) {
      ans = (ans * 2) % 1000000007;
    }
    printf("%lld\n", ans);
  }
  return 0;
}
int cmp(const void* a, const void* b) {
  if (*(long long*)a - *(long long*)b < 0) return -1;
  if (*(long long*)a - *(long long*)b > 0) return 1;
  return 0;
}
long long maxv(long long a, long long b) {
  if (a > b) return a;
  return b;
}
long long minv(long long a, long long b) {
  if (a < b) return a;
  return b;
}
long long gcd(long long u, long long v) {
  if (v == 0) return u;
  return gcd(v, u % v);
}
long long digits(long long n) { return floor(log10(n)) + 1; }
bool isPoweroftwo(long long x) { return x && (!(x & (x - 1))); }
