#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int h, w;
  cin >> h >> w;
  vector<int> vh(h), vw(w);
  for (int i = 0; i < h; ++i) cin >> vh[i];
  for (int i = 0; i < w; ++i) cin >> vw[i];
  long long ans = 1;
  bool imp = false;
  for (int i = 0; i < h && !imp; ++i) {
    for (int j = 0; j < w && !imp; ++j) {
      if ((vh[i] - 1 >= j && vw[j] == i) || (vw[j] - 1 >= i && vh[i] == j)) {
        imp = true;
        break;
      }
      if (((vw[j] < i) && (vh[i] < j))) {
        ans = (ans << 1LL) % (long long)((1e9) + 7);
      }
    }
  }
  if (imp) ans = 0;
  cout << ans % (long long)((1e9) + 7) << '\n';
  return 0;
}
