#include <bits/stdc++.h>
using namespace std;
long long kek[1200][1200];
long long mod = 1e9 + 7;
signed main() {
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long n, m;
  cin >> n >> m;
  for (long long i = 0; i < n; ++i) {
    long long r;
    cin >> r;
    for (long long j = 0; j < r; ++j) kek[i][j] = 1;
    kek[i][r] = 2;
  }
  for (long long i = 0; i < m; ++i) {
    long long r;
    cin >> r;
    for (long long j = 0; j < r; ++j) {
      if (kek[j][i] == 2) {
        cout << 0;
        return 0;
      }
      kek[j][i] = 1;
    }
    if (kek[r][i] == 1) {
      cout << 0 << endl;
      return 0;
    }
    kek[r][i] = 2;
  }
  long long res = 1;
  for (long long i = 0; i < n; ++i)
    for (long long j = 0; j < m; ++j) {
      if (!kek[i][j]) res *= 2, res %= mod;
    }
  cout << res;
  return 0;
}
