#include <bits/stdc++.h>
using namespace std;
long long int cal(long long int c) {
  long long int p = 1;
  for (int i = 0; i < c; i++) {
    p *= 2;
    p = p % 1000000007;
  }
  return p;
}
int main() {
  int n, m;
  cin >> n >> m;
  int arr[n][m], brr[n][m];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      arr[i][j] = 0;
      brr[i][j] = 0;
    }
  }
  int a[n], b[m];
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int j = 0; j < m; j++) cin >> b[j];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m && j < a[i]; j++) arr[i][j] = 1;
    if (a[i] < m) arr[i][a[i]] = 2;
  }
  for (int j = 0; j < m; j++) {
    for (int i = 0; i < n && i < b[j]; i++) brr[i][j] = 1;
    if (b[j] < n) brr[b[j]][j] = 2;
  }
  long long int c = 0, ans = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (arr[i][j] == 0 && brr[i][j] == 0) {
        c++;
      } else if (arr[i][j] == brr[i][j] || arr[i][j] == 0 || brr[i][j] == 0) {
      } else
        ans = 0;
    }
  }
  if (ans == 0) {
    cout << 0;
  } else
    cout << cal(c);
  return 0;
}
