#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000 + 123;
const int MOD = 1e9 + 7;
int ch[MAXN][MAXN];
int r[MAXN];
long long int po(int r, int x) {
  if (x == 0) return 1;
  return (long long int)r * po(r, x - 1) % MOD;
}
int main() {
  int h, w;
  cin >> h >> w;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) ch[i][j] = -1;
  for (int i = 0; i < h; i++) {
    cin >> r[i];
    for (int j = 0; j < r[i]; j++) ch[i][j] = 1;
    ch[i][r[i]] = 0;
  }
  for (int i = 0; i < w; i++) {
    int c;
    cin >> c;
    for (int j = 0; j < c; j++) {
      if (ch[j][i] != 0)
        ch[j][i] = 1;
      else {
        cout << 0;
        return 0;
      }
    }
    if (ch[c][i] == 1) {
      cout << 0;
      return 0;
    } else
      ch[c][i] = 0;
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < r[i]; j++)
      if (ch[i][j] == 0) {
        cout << 0;
        return 0;
      }
    if (ch[i][r[i]] == 1) {
      cout << 0;
      return 0;
    }
  }
  int ted = 0;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (ch[i][j] == -1) ted++;
  cout << po(2, ted);
}
