#include <bits/stdc++.h>
using namespace std;
const long long N = 200005;
const long long M = 1000000007;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t;
  t = 1;
  while (t--) {
    long long n, m;
    cin >> n >> m;
    vector<long long> vr(n), vc(m);
    for (long long i = 0; i < n; i++) cin >> vr[i];
    for (long long i = 0; i < m; i++) cin >> vc[i];
    long long ar[n][m];
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < m; j++) {
        ar[i][j] = -1;
      }
    }
    long long chk = 0;
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < vr[i]; j++) {
        ar[i][j] = 1;
      }
      if (vr[i] != m) ar[i][vr[i]] = 0;
    }
    for (long long i = 0; i < m; i++) {
      for (long long j = 0; j < vc[i]; j++) {
        if (ar[j][i] == 0) {
          chk = 1;
        } else {
          ar[j][i] = 1;
        }
      }
      if (vc[i] != n) {
        if (ar[vc[i]][i] == 1) {
          chk = 1;
        } else {
          ar[vc[i]][i] = 0;
        }
      }
    }
    if (chk == 1)
      cout << 0;
    else {
      long long cnt = 0;
      for (long long i = 0; i < n; i++) {
        for (long long j = 0; j < m; j++) {
          if (ar[i][j] == -1) cnt++;
        }
      }
      long long ans = 1;
      for (long long i = 0; i < cnt; i++) {
        ans = (ans * 2) % M;
      }
      cout << ans;
    }
  }
}
