#include <bits/stdc++.h>
using namespace std;
FILE *stream;
int a[1005][1005], r[1005], c[1005];
long long Montgomery(long long base, long long exp_1, long long mod) {
  long long int res = 1;
  while (exp_1) {
    if (exp_1 & 1) res = (res * base) % mod;
    exp_1 >>= 1;
    base = (base * base) % mod;
  }
  return res;
}
int main() {
  int h, w;
  cin >> h >> w;
  for (int i = 1; i <= h; i++) {
    cin >> r[i];
    for (int j = 1; j <= r[i]; j++) {
      if (a[i][j] == 2) {
        cout << 0;
        return 0;
      }
      a[i][j] = 1;
    }
    if (a[i][r[i] + 1] == 1) {
      cout << 0;
      return 0;
    }
    a[i][r[i] + 1] = 2;
  }
  for (int i = 1; i <= w; i++) {
    cin >> c[i];
    for (int j = 1; j <= c[i]; j++) {
      if (a[j][i] == 2) {
        cout << 0;
        return 0;
      }
      a[j][i] = 1;
    }
    if (a[c[i] + 1][i] == 1) {
      cout << 0;
      return 0;
    }
    a[c[i] + 1][i] = 2;
  }
  long long ans = 0;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      if (!a[i][j]) ans++;
    }
  }
  cout << Montgomery(2, ans, 1e9 + 7);
  return 0;
}
