#include <bits/stdc++.h>
using namespace std;
long int p(long int a1) {
  long long int ap = 1;
  while (a1) {
    ap = (ap % 1000000007) * 2;
    ap = ap % 1000000007;
    a1 = a1 - 1;
  }
  return ap;
}
int main() {
  int h, w;
  cin >> h >> w;
  int a[h][w];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) a[i][j] = 0;
  }
  long int count = 0, e = 0;
  for (int i = 0; i < h; i++) {
    int r;
    cin >> r;
    for (int j = 0; j < r; j++) {
      if (a[i][j] == 0) {
        a[i][j] = 1;
        count += 1;
      } else if (a[i][j] == 1)
        a[i][j] = 1;
      else {
        e = 1;
      }
    }
    if (r < w) {
      if (a[i][r] == 0) {
        a[i][r] = 2;
        count += 1;
      } else
        e = 1;
    }
  }
  for (int i = 0; i < w; i++) {
    int c;
    cin >> c;
    for (int j = 0; j < c; j++) {
      if (a[j][i] == 0) {
        a[j][i] = 1;
        count += 1;
      } else if (a[j][i] == 1)
        a[j][i] = 1;
      else {
        e = 1;
      }
    }
    if (c < h) {
      if (a[c][i] == 0) {
        count += 1;
        a[c][i] = 2;
      } else if (a[c][i] == 2) {
      } else
        e = 1;
    }
  }
  long int t = h * w;
  t = t - count;
  long long int ans = 1;
  ans = p(t);
  if (e == 1)
    cout << "0";
  else
    cout << ans;
  return 0;
}
