#include <bits/stdc++.h>
using namespace std;
long long pm(long long x, unsigned long long y) {
  long long res = 1;
  x = x % 1000000007;
  if (x == 0) return 0;
  while (y > 0) {
    if (y & 1) res = (res * x) % 1000000007;
    y = y >> 1;
    x = (x * x) % 1000000007;
  }
  return res;
}
int main() {
  long long n, m;
  cin >> n >> m;
  long long r[n], c[m];
  int mtx[n][m], i, j;
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      mtx[i][j] = 0;
    }
  }
  for (i = 0; i < n; i++) cin >> r[i];
  for (i = 0; i < m; i++) cin >> c[i];
  for (i = 0; i < n; i++) {
    for (j = 0; j < r[i]; j++) {
      mtx[i][j] = 1;
    }
  }
  for (i = 0; i < m; i++) {
    for (j = 0; j < c[i]; j++) {
      mtx[j][i] = 1;
    }
  }
  for (i = 0; i < n; i++) {
    if (r[i] < m && mtx[i][r[i]] == 1) {
      cout << "0\n";
      return 0;
    }
  }
  for (i = 0; i < m; i++) {
    if (c[i] < n && mtx[c[i]][i] == 1) {
      cout << "0\n";
      return 0;
    }
  }
  for (i = 0; i < n; i++) {
    for (j = 0; j <= r[i] && j < m; j++) {
      mtx[i][j] = 1;
    }
  }
  for (i = 0; i < m; i++) {
    for (j = 0; j <= c[i] && j < n; j++) {
      mtx[j][i] = 1;
    }
  }
  int count = 0;
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      if (mtx[i][j] == 0) count++;
    }
  }
  cout << pm(2, count);
  printf("\n");
  return 0;
}
