#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const long long maxn = 1e3 + 100;
long long mat[maxn][maxn];
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(9);
  long long rows, cols;
  cin >> rows >> cols;
  vector<long long> vecr(rows + 10);
  vector<long long> vecc(cols + 10);
  for (long long r = 1; r <= rows; ++r) {
    cin >> vecr[r];
  }
  for (long long c = 1; c <= cols; ++c) {
    cin >> vecc[c];
  }
  long long tot = rows * cols;
  long long one = 0;
  long long two = 0;
  long long three = 0;
  long long ok = true;
  for (long long r = 1; r <= rows; ++r) {
    mat[r][vecr[r] + 1] = -1;
  }
  for (long long c = 1; c <= cols; ++c) {
    for (long long r = 1; r <= vecc[c]; ++r) {
      if (mat[r][c] == -1) {
        ok = false;
        break;
      }
    }
  }
  memset(mat, 0, sizeof(maxn));
  for (long long c = 1; c <= cols; ++c) {
    mat[vecc[c] + 1][c] = -1;
  }
  for (long long r = 1; r <= rows; ++r) {
    for (long long c = 1; c <= vecr[r]; ++c) {
      if (mat[r][c] == -1) {
        ok = false;
        break;
      }
    }
  }
  for (long long r = 2; r <= rows; ++r) {
    for (long long c = 2; c <= cols; ++c) {
      long long y = r;
      long long x = c;
      if (x <= vecr[r] || y <= vecc[c]) {
        ++two;
      } else if (x == vecr[r] + 1) {
        ++two;
      } else if (y == vecc[c] + 1) {
        ++two;
      } else {
        ++one;
      }
    }
  }
  long long ans = 1;
  for (long long i = 1; i <= one; ++i) {
    ans *= 2;
    ans %= mod;
  }
  if (ok)
    cout << ans << "\n";
  else
    cout << "0"
         << "\n";
  return 0;
}
