#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e3 + 5;
const int MOD = 1e9 + 7;
int matrix[MAX][MAX];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int h, w;
  cin >> h >> w;
  vector<int> vh(h);
  vector<int> vw(w);
  for (int i = 0; i < h; i++) cin >> vh[i];
  for (int i = 0; i < w; i++) cin >> vw[i];
  long long int ans = 1;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (vh[i] == j and vw[j] > i) {
        cout << 0 << endl;
        return 0;
      }
      if (vw[j] == i and vh[i] > j) {
        cout << 0 << endl;
        return 0;
      } else if (vh[i] == j or vw[j] == i)
        matrix[i][j] = 0;
      else if (vh[i] > j or vw[j] > i)
        matrix[i][j] = 1;
      else
        ans = (ans * 2) % MOD;
    }
  }
  cout << ans << endl;
}
