#include <bits/stdc++.h>
using namespace std;
const int INF = 2147483647;
const long long mod = 1e9 + 7;
inline int read() {
  int x = 0, k = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') k = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 3) + (x << 1) + (c ^ 48);
    c = getchar();
  }
  return x * k;
}
long long ksm(long long a, long long b) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans = ans * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ans;
}
int h, w, c[1008], r[1008], cnt;
int main() {
  h = read(), w = read();
  for (int i = (1); i <= (h); i++) r[i] = read();
  for (int i = (1); i <= (w); i++) c[i] = read();
  for (int i = (1); i <= (h); i++) {
    for (int j = (1); j <= (w); j++) {
      if ((j == r[i] + 1 && i <= c[j]) || (j <= r[i] && i == c[j] + 1)) {
        printf("0");
        return 0;
      }
      if (j > r[i] + 1 && i > c[j] + 1) cnt++;
    }
  }
  printf("%lld", ksm(2ll, 1ll * cnt));
  return 0;
}
