#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int a[1005][1005];
int main() {
  int h, w;
  cin >> h >> w;
  vector<int> r(h), c(w);
  for (int i = 0; i < h; i++) {
    cin >> r[i];
  }
  for (int i = 0; i < w; i++) {
    cin >> c[i];
  }
  memset(a, -1, sizeof(a));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < r[i]; j++) {
      a[i][j] = 1;
    }
    a[i][r[i]] = 0;
  }
  for (int i = 0; i < w; i++) {
    for (int j = 0; j < c[i]; j++) {
      if (a[j][i] == 0) {
        cout << 0 << endl;
        return 0;
      }
      a[j][i] = 1;
    }
    if (a[c[i]][i] == 1) {
      cout << 0 << endl;
      return 0;
    }
    a[c[i]][i] = 0;
  }
  int ans = 1;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (a[i][j] == -1) {
        ans = (1ll * 2 * ans) % mod;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
