#include <bits/stdc++.h>
int const N = 1100;
int n, m, ans, ans1;
int map[N][N];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) map[i][j] = -1;
  for (int i = 1; i <= n; ++i) {
    int x;
    scanf("%d", &x);
    for (int j = 1; j <= x; ++j)
      if (map[i][j] != 0)
        map[i][j] = 1;
      else {
        printf("%d\n", 0);
        return 0;
      }
    if (map[i][x + 1] == 1) {
      printf("%d\n", 0);
      return 0;
    }
    map[i][x + 1] = 0;
  }
  for (int i = 1; i <= m; ++i) {
    int x;
    scanf("%d", &x);
    for (int j = 1; j <= x; ++j)
      if (map[j][i] != 0)
        map[j][i] = 1;
      else {
        printf("%d\n", 0);
        return 0;
      }
    if (map[x + 1][i] == 1) {
      printf("%d\n", 0);
      return 0;
    }
    map[x + 1][i] = 0;
  }
  ans = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      if (map[i][j] == -1) ans++;
  ans1 = 1;
  for (int i = 1; i <= ans; ++i) ans1 = (ans1 * 2) % 1000000007;
  printf("%d\n", ans1);
  return 0;
}
