#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w;
  scanf("%d%d", &h, &w);
  long long ans = 1;
  vector<vector<int> > a;
  a.resize(h + 1, vector<int>(w + 1));
  for (int i = 0; i < h; i++) {
    int r;
    scanf("%d", &r);
    for (int j = 0; j < r; j++) {
      a[i][j] |= 1;
    }
    a[i][r] |= 2;
  }
  for (int j = 0; j < w; j++) {
    int c;
    scanf("%d", &c);
    for (int i = 0; i < c; i++) {
      a[i][j] |= 1;
    }
    a[c][j] |= 2;
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (a[i][j] == 0)
        ans *= 2;
      else if (a[i][j] == 3)
        ans = 0;
      ans %= 1000000007;
    }
  }
  cout << ans << endl;
}
