#include <bits/stdc++.h>
using namespace std;
int a[1005][1005];
long long ksm(long long a, long long b) {
  long long re = 1;
  a = a % 1000000007;
  while (b) {
    if (b & 1) re = 1ll * re * a % 1000000007;
    a = a * a % 1000000007;
    b >>= 1;
  }
  return (re + 1000000007) % 1000000007;
}
int main() {
  int h, w;
  bool fuck = 0;
  cin >> h >> w;
  for (int i = 1; i <= h; i++) {
    int t;
    scanf("%d", &t);
    for (int j = 1; j <= t; j++) {
      a[i][j] = 1;
    }
    a[i][t + 1] = -5;
  }
  for (int i = 1; i <= w; i++) {
    int t;
    scanf("%d", &t);
    for (int j = 1; j <= t; j++) {
      if (a[j][i] == -5) {
        fuck = 1;
        break;
      }
      a[j][i] = 1;
    }
    if (a[t + 1][i] == 1) {
      fuck = 1;
      break;
    } else
      a[t + 1][i] = -5;
    if (fuck == 1) break;
  }
  if (fuck == 1) {
    cout << 0;
    return 0;
  }
  long long cnt = 0;
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++)
      if (a[i][j] == 0) cnt++;
  cout << ksm(2, cnt);
  return 0;
}
