#include <bits/stdc++.h>
using namespace std;
const long long oo = 1e15 + 7;
const long long mod = 1e9 + 7;
const int N = 1e3 + 7;
int n, m;
int h[N], w[N];
bool c[N][N];
bool a[N][N];
void Input() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) cin >> h[i];
  for (int i = 1; i <= m; i++) cin >> w[i];
  return;
}
long long _pow(int a, int b) {
  if (b == 0) return 1;
  if (b == 1) return a;
  long long t = _pow(a, b / 2) % mod;
  return (((t * t) % mod) * _pow(a, b % 2)) % mod;
}
void Solve() {
  for (int i = 1; i <= n; i++) {
    if (!h[i]) {
      a[i][1] = 0;
      c[i][1] = 1;
    } else {
      for (int j = 1; j <= h[i]; j++) {
        a[i][j] = 1;
        c[i][j] = 1;
      }
      c[i][h[i] + 1] = 1;
      a[i][h[i] + 1] = 0;
    }
  }
  for (int j = 1; j <= m; j++) {
    if (!w[j]) {
      c[1][j] = 1;
      a[1][j] = 0;
    } else {
      for (int i = 1; i <= w[j]; i++) {
        c[i][j] = 1;
        a[i][j] = 1;
      }
      c[w[j] + 1][j] = 1;
      a[w[j] + 1][j] = 0;
    }
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (!c[i][j]) cnt++;
    }
  }
  for (int i = 1; i <= n; i++) {
    int cnt = 0;
    for (int j = 1; j <= m; j++)
      if (a[i][j])
        cnt++;
      else
        break;
    if (h[i] != cnt) {
      cout << 0;
      return;
    }
  }
  for (int j = 1; j <= m; j++) {
    int cnt = 0;
    for (int i = 1; i <= n; i++)
      if (a[i][j])
        cnt++;
      else
        break;
    if (w[j] != cnt) {
      cout << 0;
      return;
    }
  }
  if (!cnt)
    cout << 1;
  else
    cout << _pow(2, cnt) % mod;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  Input();
  Solve();
  return 0;
}
