#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
int mod = 1000000007;
bool remender(long long a, long long b) { return a % b; }
void solve() {
  int n, m;
  cin >> n >> m;
  int arr[n], a[m];
  for (int i = 0; i < n; i++) cin >> arr[i];
  for (int i = 0; i < m; i++) cin >> a[i];
  int b[n][m], c[n][m];
  memset(b, 0, sizeof b);
  memset(c, 0, sizeof c);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < arr[i]; j++) b[i][j] = 1;
  }
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < a[i]; j++) {
      c[j][i] = 1;
    }
  }
  long long ways = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (c[i][j] == b[i][j] && c[i][j] == 1) {
        continue;
      }
      if (c[i][j] != b[i][j]) {
        if (c[i][j] == 1) {
          if (j == 0 || b[i][j - 1] == 1) {
            ways = 0;
          }
        } else {
          if (i == 0 || c[i - 1][j] == 1) {
            ways = 0;
          }
        }
      }
      if (c[i][j] == 0 && b[i][j] == 0) {
        if (j && i && !b[i][j - 1] && !c[i - 1][j]) {
          ways *= 2;
          ways %= mod;
        }
      }
    }
  }
  cout << ways << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  return 0;
}
