#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n, m;
  cin >> n >> m;
  vector<vector<int> > a(n, vector<int>(m, 0));
  bool bad = true;
  for (int i = 0; i < n; i++) {
    int c;
    cin >> c;
    for (int j = 0; j < c; j++) {
      if (a[i][j] == 3) bad = false;
      a[i][j] = 2;
      if (a[i][j] == 3) bad = false;
    }
    if (c < m) {
      if (a[i][c] == 2) bad = false;
      a[i][c] = 3;
    }
    if (c == 0) {
      if (a[i][0] == 2) bad = false;
      a[i][0] = 3;
    }
  }
  for (int i = 0; i < m; i++) {
    int c;
    cin >> c;
    for (int j = 0; j < c; j++) {
      if (a[j][i] == 3) bad = false;
      a[j][i] = 2;
    }
    if (c < n) {
      if (a[c][i] == 2) bad = false;
      a[c][i] = 3;
    }
    if (c == 0) {
      if (a[0][i] == 2) bad = false;
      a[0][i] = 3;
    }
  }
  if (!bad) {
    cout << 0;
    return 0;
  }
  int answer = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      if (a[i][j] == 0) answer++;
    }
  int t = 1;
  for (int i = 0; i < answer; i++) {
    t *= 2;
    t %= 1000000007;
  }
  cout << t;
  return 0;
}
