#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int n, m, x, ans = 1, flag = 0;
  cin >> n >> m;
  long long int r[n + 1];
  long long int c[m + 1];
  long long int a[n + 1][m + 1];
  for (long long int i = 0; i < n; i++) {
    cin >> r[i];
  }
  for (long long int i = 0; i < m; i++) {
    cin >> c[i];
  }
  for (long long int i = 0; i < n; i++) {
    for (long long int j = 0; j < m; j++) {
      a[i][j] = 0;
    }
  }
  for (long long int i = 0; i < n; i++) {
    for (long long int j = 0; j < r[i]; j++) {
      a[i][j] = 1;
    }
  }
  for (long long int i = 0; i < m; i++) {
    for (long long int j = 0; j < c[i]; j++) {
      a[j][i] = 1;
    }
  }
  for (long long int i = 0; i < n; i++) {
    long long int ch = 0;
    long long int x = 0;
    while (a[i][x] != 0 && x < m) {
      ch++;
      x++;
    }
    if (ch != r[i]) {
      flag = 1;
      break;
    }
  }
  if (flag == 1) {
    cout << "0";
    return 0;
  }
  for (long long int i = 0; i < m; i++) {
    long long int ch = 0;
    long long int x = 0;
    while (a[x][i] != 0 && x < n) {
      ch++;
      x++;
    }
    if (ch != c[i]) {
      flag = 1;
      break;
    }
  }
  if (flag == 1) {
    cout << "0";
    return 0;
  }
  for (long long int i = 0; i < n; i++) {
    for (long long int j = 0; j < m; j++) {
      if (r[i] < j && c[j] < i) {
        ans = (ans * 2) % 1000000007;
      }
    }
  }
  cout << ans;
  return 0;
}
