#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int H, W;
int R[1001], C[1001];
int used[1005][1005];
long long fastexp(long long b, long long exp) {
  if (exp == 0) return 1;
  long long temp = fastexp(b, exp / 2);
  temp = (temp * temp) % 1000000007;
  if (exp % 2 == 1) temp *= b;
  return temp % 1000000007;
}
int main() {
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    cin >> R[i];
    for (int j = 0; j < R[i]; j++) used[i][j] = 1;
    used[i][R[i]] = 2;
  }
  for (int i = 0; i < W; i++) {
    cin >> C[i];
    for (int j = 0; j < C[i]; j++) {
      if (used[j][i] == 2) {
        cout << 0 << endl;
        return 0;
      }
      used[j][i] = 1;
    }
    if (used[C[i]][i] == 1) {
      cout << 0 << endl;
      return 0;
    }
    used[C[i]][i] = 2;
  }
  int ans = 0;
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      if (!used[i][j]) ans++;
  cout << fastexp(2, ans) << endl;
  return 0;
}
