#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w;
  cin >> h >> w;
  int r[h], c[w];
  bool black[h][w], white[h][w];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      black[i][j] = 0;
      white[i][j] = 0;
    }
  }
  for (int i = 0; i < h; i++) {
    cin >> r[i];
    for (int j = 0; j < r[i]; j++) {
      black[i][j] = 1;
    }
    if (r[i] == w) {
      continue;
    }
    white[i][r[i]] = 1;
  }
  for (int i = 0; i < w; i++) {
    cin >> c[i];
    for (int j = 0; j < c[i]; j++) {
      black[j][i] = 1;
    }
    if (c[i] == h) {
      continue;
    }
    white[c[i]][i] = 1;
  }
  int t = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (black[i][j] && white[i][j]) {
        cout << 0 << endl;
        return 0;
      }
      if (!black[i][j] && !white[i][j]) {
        t++;
      }
    }
  }
  long long ans = 1;
  if (t == 0) {
    cout << 1 << endl;
    return 0;
  }
  for (int i = 0; i < t; i++) {
    ans = (ans * 2) % 1000000007;
  }
  cout << ans << endl;
  return 0;
}
