#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const int iNF = 0x3f3f3f3f;
long long binpow(long long a, long long b, long long m) {
  a %= m;
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}
int field[1005][1005];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int h, w, NotCheck = 0;
  bool fail = 0;
  cin >> h >> w;
  memset(field, -1, sizeof(field));
  for (int i = 1; i <= h; i++) {
    int r;
    cin >> r;
    for (int j = 1; j <= r; j++) {
      field[i][j] = 1;
    }
    field[i][r + 1] = 0;
  }
  for (int j = 1; j <= w; j++) {
    int c;
    cin >> c;
    for (int i = 1; i <= c; i++) {
      if (field[i][j] == -1)
        field[i][j] = 1;
      else if (field[i][j] != 1)
        fail = 1;
    }
    if (field[c + 1][j] == -1)
      field[c + 1][j] = 0;
    else if (field[c + 1][j] != 0)
      fail = 1;
  }
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      if (field[i][j] == -1) NotCheck++;
    }
  }
  if (fail)
    cout << "0\n";
  else
    cout << binpow(2, NotCheck, 1000000007) << '\n';
  return 0;
}
