#include <bits/stdc++.h>
using namespace std;
long long power(long long a, long long b, long long mod) {
  if (b == 0) return 1;
  long long val = power(a, b >> 1, mod);
  long long ans = ((val % mod) * (val % mod)) % mod;
  if (b % 2 != 0) ans = ((ans % mod) * (a % mod)) % mod;
  return ans;
}
int m[1002][1002] = {0};
int m2[1002][1002] = {0};
int main(void) {
  int h, w;
  cin >> h >> w;
  vector<int> R, C;
  for (int i = 0; i < h; i++) {
    int r;
    cin >> r;
    for (int j = 0; j < r; j++) m[i][j] = 1;
    R.push_back(r);
  }
  for (int i = 0; i < w; i++) {
    int r;
    cin >> r;
    for (int j = 0; j < r; j++) m[j][i] = 1;
    C.push_back(r);
  }
  for (int i = 0; i < h; i++) {
    int c = 0;
    for (int j = 0; j < w; j++) {
      if (m[i][j] == 0) break;
      c++;
    }
    if (c != R[i]) {
      cout << 0 << endl;
      return 0;
    }
  }
  for (int i = 0; i < w; i++) {
    int c = 0;
    for (int j = 0; j < h; j++) {
      if (m[j][i] == 0) break;
      c++;
    }
    if (c != C[i]) {
      cout << 0 << endl;
      return 0;
    }
  }
  long long cnt = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (m[i][j] == 0) {
        for (int l = j + 1; l < w; l++) {
          if (m[i][l] != 1) m2[i][l]++;
        }
        break;
      }
    }
  }
  for (int i = 0; i < w; i++) {
    for (int j = 0; j < h; j++) {
      if (m[j][i] == 0) {
        for (int l = j + 1; l < h; l++) {
          if (m[l][i] != 1) m2[l][i]++;
        }
        break;
      }
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++)
      if (m2[i][j] == 2) cnt++;
  }
  cout << power(2, cnt, 1000000007) << endl;
  return 0;
}
