#include <bits/stdc++.h>
using namespace std;
int main() {
  int r[1100];
  int ans = 1, c[1100];
  int x = 0, h, w;
  cin >> h >> w;
  int g;
  char a[1100][1100];
  memset(a, '.', sizeof a);
  for (int i = 1; i <= h; i++) {
    cin >> r[i];
  }
  for (int j = 1; j <= w; j++) {
    cin >> c[j];
  }
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      if (j <= r[i]) {
        a[i][j] = 'B';
      }
      if (j == r[i] + 1) {
        a[i][j] = 'W';
      }
      if (i <= c[j]) {
        if (a[i][j] == 'W') {
          cout << 0;
          return 0;
        }
        a[i][j] = 'B';
      }
      if (i == c[j] + 1) {
        if (a[i][j] == 'B') {
          cout << 0;
          return 0;
        }
        a[i][j] = 'W';
      }
      if (a[i][j] != 'W' && a[i][j] != 'B') {
        ans *= 2;
        ans %= 1000000007;
      }
    }
  }
  cout << ans;
}
