#include <bits/stdc++.h>
using namespace std;
long long powe(long long a, long long b) {
  long long ans = 1;
  a = a % 1000000007;
  while (b > 0) {
    if (b & 1) ans = (ans * a) % 1000000007;
    b = b >> 1;
    a = (a * a) % 1000000007;
  }
  return ans % 1000000007;
}
int main() {
  long long a, b, x;
  cin >> a >> b;
  long long arr[a + 1][b + 1];
  for (long long i = 0; i <= a; i++)
    for (long long j = 0; j <= b; j++) arr[i][j] = -1;
  for (long long i = 1; i <= a; i++) {
    cin >> x;
    for (long long j = 1; j <= x; j++) {
      if (arr[i][j] == 1 || arr[i][j] == -1)
        arr[i][j] = 1;
      else {
        cout << "1";
        return 0;
      }
    }
    if (x < b && (arr[i][x + 1] == 0 || arr[i][x + 1] == -1))
      arr[i][x + 1] = 0;
    else if (x < b) {
      cout << "0";
      return 0;
    }
  }
  for (long long i = 1; i <= b; i++) {
    cin >> x;
    for (long long j = 1; j <= x; j++) {
      if (arr[j][i] == 1 || arr[j][i] == -1)
        arr[j][i] = 1;
      else {
        cout << "0";
        return 0;
      }
    }
    if (x < a && (arr[x + 1][i] == 0 || arr[x + 1][i] == -1))
      arr[x + 1][i] = 0;
    else if (x < a) {
      cout << "0";
      return 0;
    }
  }
  long long cnt = 0;
  for (long long i = 1; i <= a; i++)
    for (long long j = 1; j <= b; j++)
      if (arr[i][j] == -1) cnt++;
  cout << powe(2, cnt) % 1000000007 << endl;
  ;
}
