#include <bits/stdc++.h>
using namespace std;
long long expo(long long base, long long exponent, long long mod) {
  long long ans = 1;
  while (exponent != 0) {
    if ((exponent & 1) == 1) {
      ans = ans * base;
      ans = ans % mod;
    }
    base = base * base;
    base %= mod;
    exponent >>= 1;
  }
  return ans % mod;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int TESTS = 1;
  while (TESTS--) {
    long long h, w;
    cin >> h >> w;
    vector<long long int> r(h), c(w);
    for (long long int i = 0; i < h; i++) cin >> r[i];
    for (long long int i = 0; i < w; i++) cin >> c[i];
    set<pair<long long, long long> > s;
    long long ans = 0;
    for (long long int i = 0; i < h; i++) {
      if (r[i] != w && c[r[i]] > i) {
        cout << 0;
        return 0;
      }
    }
    for (long long int i = 0; i < w; i++) {
      if (c[i] != h && r[c[i]] > i) {
        cout << 0;
        return 0;
      }
    }
    for (long long int i = 0; i < h; i++) {
      for (long long int j = 0; j < w; j++) {
        if ((j > r[i]) && (i > c[j])) ans++;
      }
    }
    cout << expo(2, ans, 1000000007);
  }
  return 0;
}
