#include <bits/stdc++.h>
using namespace std;
long long power(long long i, long long j) {
  if (j == 0) return 1;
  long long t = power(i, j / 2) % 1000000007;
  if (j % 2 == 0)
    return (t % 1000000007 * t % 1000000007) % 1000000007;
  else
    return ((t % 1000000007 * t % 1000000007) % 1000000007 * (i % 1000000007) %
            1000000007) %
           1000000007;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  long long n, m;
  cin >> n >> m;
  long long r[n][m];
  long long s[n][m];
  vector<long long> row, col;
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < m; j++) {
      r[i][j] = 0;
      s[i][j] = 0;
    }
  }
  for (long long i = 0; i < n; i++) {
    long long x;
    cin >> x;
    row.push_back(x);
    for (long long j = 0; j < x; j++) {
      r[i][j] = 1;
      s[i][j] = 1;
    }
  }
  for (long long j = 0; j < m; j++) {
    long long x;
    cin >> x;
    col.push_back(x);
    for (long long i = 0; i < x; i++) {
      r[i][j] = 1;
      s[i][j] = 1;
    }
  }
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < m; j++) {
      if (r[i][j] != 1) {
        r[i][j] = 2;
        break;
      }
    }
  }
  for (long long j = 0; j < m; j++) {
    for (long long i = 0; i < n; i++) {
      if (r[i][j] != 1) {
        r[i][j] = 2;
        break;
      }
    }
  }
  long long c = 0;
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < m; j++) {
      if (r[i][j] == 0) c++;
    }
  }
  long long f = 0;
  for (long long i = 0; i < n; i++) {
    long long cnt = 0;
    for (long long j = 0; j < m; j++) {
      if (s[i][j] == 0) break;
      cnt++;
    }
    if (row[i] != cnt) f = 1;
  }
  for (long long j = 0; j < m; j++) {
    long long cnt = 0;
    for (long long i = 0; i < n; i++) {
      if (s[i][j] == 0) break;
      cnt++;
    }
    if (col[j] != cnt) f = 1;
  }
  if (f == 1)
    cout << "0" << endl;
  else
    cout << power(2, c);
}
