#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
using namespace std;
const long long SIZE = 5 * 1e5, MOD = 1e9 + 7;
vector<long long> vec, vec2;
long long matr[1001][1001];
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long h, w;
  cin >> h >> w;
  vec.resize(h);
  for (long long i = 0; i < h; i++) cin >> vec[i];
  vec2.resize(w);
  for (long long i = 0; i < w; i++) cin >> vec2[i];
  for (long long i = 0; i < h; i++) {
    for (long long j = 0; j < w; j++) {
      matr[i][j] = -1;
    }
  }
  for (long long i = 0; i < h; i++) {
    for (long long j = 0; j < vec[i]; j++) {
      matr[i][j] = 1;
    }
    matr[i][vec[i]] = 0;
  }
  bool flag = false;
  for (long long i = 0; i < w; i++) {
    for (long long j = 0; j < vec2[i]; j++) {
      if (matr[j][i] == 0) flag = true;
      matr[j][i] = 1;
    }
    if (matr[vec2[i]][i] == 1) flag = true;
    matr[vec2[i]][i] = 0;
  }
  if (flag) {
    cout << "0";
    return 0;
  }
  long long ans = 1;
  for (long long i = 0; i < h; i++) {
    for (long long j = 0; j < w; j++) {
      if (matr[i][j] == -1) ans *= 2;
      ans %= MOD;
    }
  }
  cout << ans;
  return 0;
}
