#include <bits/stdc++.h>
using namespace std;
long f[1003][1003], r[1003], c[1003], d[1003][1003];
long m, n, dem;
long long luythua(long long a, long long b) {
  if (b == 0) return 1;
  long long m = luythua(a, b / 2);
  if (b % 2 == 0)
    return (m * m) % 1000000007;
  else
    return (((m * m) % 1000000007) * a) % 1000000007;
}
int main() {
  dem = 0;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) cin >> r[i];
  for (int i = 1; i <= m; i++) cin >> c[i];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= r[i]; j++) d[i][j] = 11;
  for (int i = 1; i <= m; i++)
    for (int j = 1; j <= c[i]; j++) d[j][i] = 11;
  for (int i = 1; i <= n; i++)
    if (d[i][r[i] + 1] == 11) {
      cout << 0;
      return 0;
    }
  for (int i = 1; i <= m; i++)
    if (d[c[i] + 1][i] == 11) {
      cout << 0;
      return 0;
    }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (j <= r[i] + 1)
        f[i][j] = 1;
      else
        f[i][j] = 0;
  for (int i = 1; i <= m; i++)
    for (int j = 1; j <= n; j++)
      if (j <= c[i] + 1) f[j][i] = 1;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (f[i][j] == 0) dem++;
  cout << luythua(2, dem);
}
