#include <bits/stdc++.h>
using namespace std;
long long modExp(int a, int b, int mod) {
  if (b == 0) return 1;
  if (b == 1) return a % mod;
  if (b % 2 == 0)
    return (modExp(a, b / 2, mod) * modExp(a, b / 2, mod)) % mod;
  else
    return (modExp(a, b / 2, mod) * modExp(a, b / 2, mod) * modExp(a, 1, mod)) %
           mod;
}
int main() {
  char mat[1005][1005];
  int h, w, cnt = 0;
  int c[1005] = {0}, r[1005] = {0};
  bool impossible = false;
  cin >> h >> w;
  for (int i = 0; i < h; ++i) cin >> r[i];
  for (int i = 0; i < w; ++i) cin >> c[i];
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < r[i]; ++j) {
      mat[i][j] = 1;
    }
    if (r[i] < w) mat[i][r[i]] = -1;
  }
  for (int i = 0; i < w; ++i) {
    if (c[i] == 0 && mat[0][i] == 1) {
      impossible = true;
      break;
    }
    for (int j = 0; j < c[i]; ++j) {
      if (mat[j][i] == -1) {
        impossible = true;
        break;
      } else {
        mat[j][i] = 1;
      }
    }
    if (mat[c[i]][i] == 1) {
      impossible = true;
      break;
    }
    if (impossible) break;
    if (c[i] < h) mat[c[i]][i] = -1;
  }
  if (impossible) {
    cout << "0" << endl;
    return 0;
  }
  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j) {
      if (mat[i][j] == 0) cnt++;
    }
  printf("%lld\n", modExp(2, cnt, 1000000007));
  return 0;
}
