#include <bits/stdc++.h>
using namespace std;
long long inf = 100000000;
int main() {
  int h, w;
  cin >> h >> w;
  int i = 0;
  int ha[h], wa[w], a;
  bool mat[h][w];
  int j;
  while (i < h) {
    j = 0;
    while (j < w) {
      mat[i][j] = false;
      j++;
    }
    i++;
  }
  i = 0;
  while (i < h) {
    cin >> a;
    ha[i] = a;
    j = 0;
    while (j < a) {
      mat[i][j] = true;
      j++;
    }
    i++;
  }
  j = 0;
  while (j < w) {
    cin >> a;
    wa[j] = a;
    i = 0;
    while (i < a) {
      mat[i][j] = true;
      i++;
    }
    j++;
  }
  i = 0;
  long long ans = 1;
  while (i < h) {
    if (ha[i] < w) {
      if (mat[i][ha[i]]) break;
    }
    i++;
  }
  if (i < h) {
    ans = 0;
  }
  j = 0;
  while (j < w) {
    if (wa[j] < h) {
      if (mat[wa[j]][j]) break;
    }
    j++;
  }
  if (j < w) ans = 0;
  i = 0;
  while (i < h) {
    if (ha[i] < w) mat[i][ha[i]] = true;
    i++;
  }
  j = 0;
  while (j < w) {
    if (wa[j] < h) mat[wa[j]][j] = true;
    j++;
  }
  i = 0;
  while (i < h) {
    j = 0;
    while (j < w) {
      if (!mat[i][j]) {
        ans = ans * 2;
        ans %= 1000000007;
      }
      j++;
    }
    i++;
  }
  cout << ans << endl;
  return 0;
}
