#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000007;
const int MOD = 1000000007;
const int nmax = 1012;
int h, w, r[nmax], c[nmax], a[nmax][nmax], flg;
long long res;
long long ksm(long long a, int b) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans = ans * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return ans;
}
int main() {
  scanf("%d%d", &h, &w);
  for (int i = 0; i < h; ++i) scanf("%d", &r[i]);
  for (int i = 0; i < w; ++i) scanf("%d", &c[i]);
  memset(a, -1, sizeof a);
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < r[i]; ++j) a[i][j] = 1;
    a[i][r[i]] = 0;
  }
  for (int i = 0; i < w; ++i) {
    for (int j = 0; j < c[i]; ++j) {
      if (a[j][i] == 0) {
        flg = 1;
        break;
      }
      a[j][i] = 1;
    }
    if (flg == 1) break;
    if (a[c[i]][i] == 1) {
      flg = 1;
      break;
    }
    a[c[i]][i] = 0;
  }
  if (flg == 0) {
    int cnt = 0;
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        if (a[i][j] == -1) ++cnt;
      }
    }
    res = ksm(2, cnt);
  }
  printf("%I64d\n", res);
}
