#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 20;
const long long INF = LONG_MAX;
long long a[1010][1010];
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  for (long long i = 0; i <= 1005; ++i)
    for (long long j = 0; j <= 1005; ++j) a[i][j] = -1;
  long long n, m;
  cin >> n >> m;
  for (long long i = 1; i <= n; ++i) {
    long long x;
    cin >> x;
    for (long long j = 1; j <= x; ++j) {
      if (a[i][j] == 0) {
        cout << 0;
        return 0;
      }
      a[i][j] = 1;
    }
    if (a[i][x + 1] == 1) {
      cout << 0;
      return 0;
    }
    a[i][x + 1] = 0;
  }
  for (long long i = 1; i <= m; ++i) {
    long long x;
    cin >> x;
    for (long long j = 1; j <= x; ++j) {
      if (a[j][i] == 0) {
        cout << 0;
        return 0;
      }
      a[j][i] = 1;
    }
    if (a[x + 1][i] == 1) {
      cout << 0;
      return 0;
    }
    a[x + 1][i] = 0;
  }
  long long mod = 1e9 + 7;
  long long cnt = 0;
  for (long long i = 1; i <= n; ++i) {
    for (long long j = 1; j <= m; ++j) {
      if (a[i][j] == -1) cnt++;
    }
  }
  long long ans = 1;
  while (cnt--) {
    ans *= 2LL;
    ans %= mod;
  }
  cout << (ans + mod) % mod << endl;
  return 0;
}
