#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w;
  int r[1005], c[1005];
  cin >> h >> w;
  for (int i = 0; i < h; i++) cin >> r[i];
  for (int i = 0; i < w; i++) cin >> c[i];
  bool Existed = true;
  long long ans = 1;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      if (r[i] > j && c[j] == i || r[i] == j && c[j] > i) {
        Existed = false;
      }
    }
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      if (i > c[j] && j > r[i]) {
        ans = ans * 2 % 1000000007;
      }
    }
  if (Existed)
    cout << ans << endl;
  else
    cout << 0 << endl;
}
