#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> r(n);
  vector<int> c(m);
  for (int i = 0; i < n; ++i) cin >> r[i];
  for (int i = 0; i < m; ++i) cin >> c[i];
  vector<vector<int> > v(n, vector<int>(m, -1));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < r[i]; j++) v[i][j] = 1;
    if (r[i] < m) v[i][r[i]] = 0;
  }
  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < c[i]; j++) {
      if (v[j][i] == 0) {
        cout << 0;
        return 0;
      }
      v[j][i] = 1;
    }
    if (c[i] < n) {
      if (v[c[i]][i] == 1) {
        cout << 0;
        return 0;
      }
      v[c[i]][i] = 0;
    }
  }
  int count = 0;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      if (v[i][j] == -1) ++count;
  int ans = 1;
  for (int i = 0; i < count; ++i) {
    ans *= 2;
    ans %= 1000000007;
  }
  cout << ans;
}
