#include <bits/stdc++.h>
using namespace std;
const long long int md = 998244353;
const long long MAX = LONG_MAX;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long h, w;
  cin >> h >> w;
  long long r[h], c[w];
  for (long long i = 0; i < h; i++) cin >> r[i];
  for (long long i = 0; i < w; i++) cin >> c[i];
  long long ar[h][w];
  for (long long i = 0; i < h; i++)
    for (long long j = 0; j < w; j++) ar[i][j] = -1;
  for (long long i = 0; i < h; i++) {
    long long kk = 0;
    if (r[i] == 0) {
      ar[i][0] = 0;
      continue;
    }
    for (long long j = 0; j < w; j++) {
      if (kk == r[i]) {
        ar[i][j] = 0;
        break;
      }
      kk++;
      ar[i][j] = 1;
    }
  }
  for (long long i = 0; i < w; i++) {
    long long kk = 0;
    if (c[i] == 0) {
      if (ar[0][i] != 1) {
        ar[0][i] = 0;
      } else
        ar[0][i] = -2;
      continue;
    }
    for (long long j = 0; j < h; j++) {
      if (kk == c[i]) {
        if (ar[j][i] != 1)
          ar[j][i] = 0;
        else
          ar[j][i] = -2;
        break;
      }
      kk++;
      if (ar[j][i] != 0)
        ar[j][i] = 1;
      else
        ar[j][i] = -2;
    }
  }
  long long prod = 1;
  for (long long i = 0; i < h; i++)
    for (long long j = 0; j < w; j++) {
      if (ar[i][j] == -2)
        prod *= 0;
      else if (ar[i][j] == -1)
        prod = (prod * 2) % (long long)(1e9 + 7);
    }
  cout << prod;
  return 0;
}
