#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
int power(long long x, unsigned int y) {
  int res = 1;
  x = x % MOD;
  while (y > 0) {
    if (y & 1) res = (res * x) % MOD;
    y = y >> 1;
    x = (x * x) % MOD;
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  bool isValid = true;
  int **arr = new int *[n];
  for (long long i = 0; i < n; i++) arr[i] = new int[m];
  for (long long i = 0; i < n; i++)
    for (long long j = 0; j < m; j++) arr[i][j] = -1;
  for (long long i = 0; i < n; i++) {
    int temp;
    cin >> temp;
    long long j = 0;
    for (j = 0; j < temp; j++) arr[i][j] = 1;
    if (j < m) arr[i][j] = 0;
  }
  for (long long i = 0; i < m; i++) {
    int temp;
    cin >> temp;
    long long j = 0;
    for (j = 0; j < temp; j++) {
      if (arr[j][i] == -1 || arr[j][i] == 1) {
        arr[j][i] = 1;
      } else {
        isValid = false;
        break;
      }
    }
    if (j < n) {
      if (arr[j][i] == -1 || arr[j][i] == 0) {
        arr[j][i] = 0;
      } else {
        isValid = false;
      }
    }
  }
  if (isValid) {
    long long count = 0;
    for (long long i = 0; i < n; i++)
      for (long long j = 0; j < m; j++)
        if (arr[i][j] == -1) count++;
    cout << power(2, count) << endl;
  } else {
    cout << 0 << endl;
  }
  return 0;
}
