#include <bits/stdc++.h>
using namespace std;
int a[1002][1002];
int n, m, i, j, x, sol;
int main() {
  cin >> n >> m;
  memset(a, -1, sizeof a);
  for (i = 1; i <= n; i++) {
    cin >> x;
    if (!x) {
      a[i][1] = 0;
      continue;
    }
    for (j = 1; j <= x; j++) a[i][j] = 1;
    a[i][x + 1] = 0;
  }
  for (j = 1; j <= m; j++) {
    cin >> x;
    if (!x) {
      if (a[1][j] == 1) {
        cout << 0;
        return 0;
      }
      a[1][j] = 0;
      continue;
    }
    for (i = 1; i <= x; i++) {
      if (a[i][j] == 0) {
        cout << 0;
        return 0;
      }
      a[i][j] = 1;
    }
    if (a[x + 1][j] == 1) {
      cout << 0;
      return 0;
    }
    a[x + 1][j] = 0;
  }
  int sol = 1;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++)
      if (a[i][j] == -1) sol = (sol * 2) % 1000000007;
  cout << sol;
  return 0;
}
