#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w;
  cin >> h >> w;
  long long count = 1;
  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];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (r[i] == j && c[j] > i) {
        cout << 0;
        exit(0);
      } else if (i == c[j] && r[i] > j) {
        cout << 0;
        exit(0);
      } else if (j > r[i] && i > c[j]) {
        count *= 2;
        count %= 1000000007;
      }
    }
  }
  cout << count;
}
