#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 7;
int nn[1007], mm[1007], mp[1007][1007];
long long quick_power(long long b) {
  long long a = 2;
  long long res = 1;
  while (b) {
    if (b & 1) res = res * a % 1000000007;
    a = a * a % 1000000007;
    b >>= 1;
  }
  return res % 1000000007;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  int flag = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &nn[i]);
    for (int j = 1; j <= nn[i]; j++) mp[i][j] = 1;
    mp[i][nn[i] + 1] = -1;
  }
  for (int j = 1; j <= m; j++) {
    scanf("%d", &mm[j]);
    if (flag) continue;
    for (int i = 1; i <= mm[j]; i++) {
      if (mp[i][j] == -1) flag = 1;
      mp[i][j] = 1;
    }
    if (mp[mm[j] + 1][j] == 1)
      flag = 1;
    else
      mp[mm[j] + 1][j] = -1;
  }
  if (flag) {
    printf("0\n");
    return 0;
  }
  long long cnt = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (mp[i][j] == 0) cnt++;
  long long ans = quick_power(cnt);
  printf("%lld\n", ans);
  return 0;
}
