#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
map<long long, long long> mp;
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long h, w;
  cin >> h >> w;
  long long a[h][w];
  memset(a, 0, sizeof a);
  for (long long i = 0; i < h; i++) {
    long long num;
    cin >> num;
    for (long long j = 0; j < num; j++) {
      a[i][j] = 1;
    }
    if (num < w) a[i][num] = -1;
  }
  long long fl = 0;
  for (long long i = 0; i < w; i++) {
    long long num;
    cin >> num;
    for (long long j = 0; j < num; j++) {
      if (a[j][i] == -1) {
        fl = 1;
        break;
      }
      a[j][i] = 1;
    }
    if (fl) {
      break;
    }
    if (num < h) {
      if (a[num][i] == 1) {
        fl = 1;
        break;
      }
      a[num][i] = -1;
    }
    if (fl) {
      break;
    }
  }
  if (fl) {
    cout << 0;
    return 0;
  }
  long long cnt = 0;
  for (long long i = 0; i < h; i++) {
    for (long long j = 0; j < w; j++) {
      if (a[i][j] == 0) cnt++;
    }
  }
  long long res = 1;
  while (cnt > 0) {
    res = (res * 2) % (long long)1000000007;
    cnt--;
  }
  cout << res << "\n";
  return 0;
}
