#include <bits/stdc++.h>
using namespace std;
int s[1007][1007] = {0};
long long fpow(long long a, long long n) {
  long long base = a;
  long long ans = 1;
  while (n) {
    if (n % 2 == 1) ans = ans * base % 1000000007;
    base = base * base % 1000000007;
    n /= 2;
  }
  return ans;
}
int main() {
  long long h, w, r, c, i, j;
  scanf("%lld%lld", &h, &w);
  for (i = 1; i <= h; i++) {
    scanf("%lld", &r);
    for (j = 1; j <= r; j++) {
      s[i][j] = 2;
    }
    s[i][j] = 1;
  }
  for (i = 1; i <= w; i++) {
    scanf("%lld", &c);
    for (j = 1; j <= c; j++) {
      if (s[j][i] == 1) {
        printf("0\n");
        return 0;
      }
      s[j][i] = 2;
    }
    if (s[j][i] == 2) {
      printf("0\n");
      return 0;
    }
    s[j][i] = 1;
  }
  long long cnt = 0;
  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w; j++) {
      if (s[i][j] == 0) {
        cnt++;
      }
    }
  }
  printf("%lld\n", fpow(2, cnt));
  return 0;
}
