#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int h, w;
  cin >> h >> w;
  vector<long long int> r(h, 0);
  vector<long long int> c(w, 0);
  for (long long int i = 0; i < h; i++) {
    cin >> r[i];
  }
  for (long long int i = 0; i < w; i++) {
    cin >> c[i];
  }
  long long int count = 0;
  long long int exsist = 0;
  for (long long int i = 0; i < h; i++) {
    long long int x = r[i];
    if (x < w && c[x] >= i + 1) {
      exsist = -1;
      break;
    }
  }
  for (long long int i = 0; i < w; i++) {
    long long int x = c[i];
    if (x < h && r[x] >= i + 1) {
      exsist = -1;
      break;
    }
  }
  for (long long int i = 0; i < w; i++) {
    if (i == c[i] && c[i] < h && r[c[i]] >= i + 1) {
      exsist = -1;
      continue;
    }
  }
  for (long long int i = 0; i < h; i++) {
    for (long long int j = r[i] + 1; j < w; j++) {
      if (c[j] + 1 <= i) count++;
    }
  }
  long long int mul = 1;
  for (long long int i = 1; i <= count; i++) {
    mul = mul * 2;
    mul = mul % (1000000007);
  }
  if (exsist == -1)
    cout << "0"
         << "\n";
  else {
    cout << mul << "\n";
  }
}
