#include <bits/stdc++.h>
using namespace std;
int c[10000], kq, r[10000], hang, cot, h[10000], a[3000][3000], mod = 1e9 + 7;
int main() {
  cin >> hang >> cot;
  for (int i = 1; i <= hang; i++) cin >> r[i];
  for (int i = 1; i <= cot; i++) cin >> c[i];
  for (int i = 1; i <= hang; i++) {
    for (int e = 1; e <= r[i]; e++) a[i][e] = 1;
  }
  for (int i = 1; i <= hang; i++) a[i][0] = 1;
  for (int i = 1; i <= cot; i++) {
    for (int e = 1; e <= c[i]; e++) {
      if (a[e][i] != 1 && a[e][i - 1] == 1) {
        cout << 0;
        exit(0);
      }
      a[e][i] = 2;
    }
  }
  for (int i = 1; i <= hang; i++) {
    int d = 0;
    for (int e = 1; e <= cot; e++) {
      if (a[i][e] == 0) break;
      d++;
    }
    if (d != r[i]) {
      cout << 0;
      exit(0);
    }
  }
  for (int i = 1; i <= cot; i++) {
    int d = 0;
    for (int e = 1; e <= hang; e++) {
      if (a[e][i] == 0) break;
      d++;
    }
    if (d != c[i]) {
      cout << 0;
      exit(0);
    }
  }
  for (int i = 1; i <= hang; i++) {
    for (int e = 1; e <= cot; e++) {
      if (i != 1 && e != 1)
        if (r[i] + 1 < e && c[e] + 1 < i) kq++;
    }
  }
  long long kq1 = 1;
  for (long long i = 1; i <= kq; i++) kq1 *= 2, kq1 %= mod;
  cout << kq1 << endl;
}
