#include <bits/stdc++.h>
using namespace std;
void err(istream_iterator<string> it) { cerr << endl; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << " ";
  err(++it, args...);
}
long long mod(long long x, long long m = 1000000007) { return x % m; }
long long power(long long x, long long y, long long p = 1000000007) {
  x %= p;
  long long val = 1;
  while (y) {
    if (y & 1LL) val = (val * x) % p;
    y >>= 1LL;
    x = (x * x) % p;
  }
  return val;
}
long long r[1001];
long long c[1001];
long long a[1001][1001];
long long b[1001][1001];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long h, w;
  cin >> h >> w;
  for (int i = 1; i <= h; i++) cin >> r[i];
  for (int i = 1; i <= w; i++) cin >> c[i];
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= r[i]; j++) a[i][j] = 1;
  }
  for (int i = 1; i <= w; i++) {
    for (int j = 1; j <= c[i]; j++) a[j][i] = 1;
  }
  for (int i = 1; i <= h; i++) {
    int f = 0;
    for (int j = 1; j <= w; j++) {
      if (a[i][j] == 0) break;
      f++;
    }
    if (f != r[i]) {
      cout << 0 << '\n';
      return 0;
    }
  }
  for (int i = 1; i <= w; i++) {
    int f = 0;
    for (int j = 1; j <= h; j++) {
      if (a[j][i] == 0) break;
      f++;
    }
    if (f != c[i]) {
      cout << 0 << '\n';
      return 0;
    }
  }
  for (int i = 1; i <= h; i++) {
    int f = 0;
    for (int j = 1; j <= w; j++) {
      if (a[i][j]) continue;
      if (!f) {
        b[i][j] = 1;
        f = 1;
        continue;
      }
    }
  }
  long long ans = 0;
  for (int j = 1; j <= w; j++) {
    int f = 0;
    for (int i = 1; i <= h; i++) {
      if (a[i][j]) continue;
      if (!f) {
        b[i][j] = 1;
        f = 1;
        continue;
      }
      if (b[i][j] == 0) ans++;
    }
  }
  cout << power(2, ans) << '\n';
}
