#include <bits/stdc++.h>
using namespace std;
int r[1005], c[1005];
int re[1005][1005];
int main() {
  int h, w;
  scanf("%d%d", &h, &w);
  for (int i = 1; i <= h; i++) scanf("%d", r + i);
  for (int i = 1; i <= w; i++) scanf("%d", c + i);
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= r[i]; j++) {
      re[i][j] = 1;
    }
    re[i][r[i] + 1] = 2;
  }
  for (int i = 1; i <= w; i++) {
    for (int j = 1; j <= c[i]; j++) {
      if (re[j][i] == 2) {
        printf("0\n");
        return 0;
      }
      re[j][i] = 1;
    }
    if (re[c[i] + 1][i] == 1) {
      printf("0\n");
      return 0;
    }
    re[c[i] + 1][i] = 2;
  }
  long long fi = 1;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      if (!re[i][j]) fi = (fi + fi) % 1000000007;
    }
    fi = fi % 1000000007;
  }
  printf("%lld\n", fi);
  return 0;
}
