#include <bits/stdc++.h>
using namespace std;
inline void read(int &x) {
  x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    c = getchar();
  }
  while (c <= '9' && c >= '0') {
    x = ((x << 1) + (x << 3)) + c - '0';
    c = getchar();
  }
  return;
}
int a[1005][1005];
int n, m;
int b[1005], c[1005];
int fastpow(long long a, long long b) {
  long long tmp = a, ans = 1;
  while (b) {
    if (b & 1) ans = (ans * tmp) % 1000000007;
    tmp = (tmp * tmp) % 1000000007;
    b >>= 1;
  }
  return ans;
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) a[i][j] = 2;
  for (int i = 1; i <= n; i++) cin >> b[i];
  for (int i = 1; i <= m; i++) cin >> c[i];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= b[i]; j++) {
      if (a[i][j] == 0) {
        cout << 0;
        return 0;
      }
      a[i][j] = 1;
    }
    if (a[i][b[i] + 1] == 1) {
      cout << 0;
      return 0;
    }
    a[i][b[i] + 1] = 0;
  }
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= c[i]; j++) {
      if (a[j][i] == 0) {
        cout << 0;
        return 0;
      }
      a[j][i] = 1;
    }
    if (a[c[i] + 1][i] == 1) {
      cout << 0;
      return 0;
    }
    a[c[i] + 1][i] = 0;
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      if (a[i][j] == 2) cnt++;
    }
  cout << fastpow(2, cnt);
}
