#include <bits/stdc++.h>
using namespace std;
int h, w, i, j, mod = 1e9 + 7, r, c, ans = 1, a[1001][1001];
int main() {
  cin.tie(NULL);
  ios_base::sync_with_stdio(0);
  cin >> h >> w;
  for (i = 0; i < h; i++) {
    cin >> r;
    for (j = 0; j < r; j++) a[i][j] = 2;
    a[i][r] = 1;
  }
  for (i = 0; i < w; i++) {
    cin >> c;
    for (j = 0; j < c; j++)
      if (a[j][i] == 1) {
        cout << 0 << endl;
        return 0;
      } else
        a[j][i] = 2;
    if (a[c][i] == 2) {
      cout << 0 << endl;
      return 0;
    }
    a[c][i] = 1;
  }
  for (i = 0; i < h; i++)
    for (j = 0; j < w; j++)
      if (!a[i][j]) ans = (ans * 2) % mod;
  cout << ans << endl;
  return 0;
}
