#define _CRT_SECURE_NO_WARNINGS
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
#include <bits/stdc++.h>
using namespace std;
#define CRP(t, x) const t &x
#define OPL(t, x) bool operator<(CRP(t, x)) const
#define FIL(x, v) memset(x, v, sizeof(x))
#define CLR(x) FIL(x, 0)
#define NE1(x) FIL(x, -1)
#define INF(x) FIL(x, 0x3f)
#ifndef _DEBUG
#define _DEBUG 0
#endif // !_DEBUG
#define IFD if (_DEBUG)
typedef long long ll, i64;
const int N = 1050, mod = 1e9 + 7;
int a[N][N], r[N], c[N];
void trySet(int i, int j, int v)
{
    if (a[i][j] != 0 && a[i][j] != v) puts("0"), exit(0);
    a[i][j] = v;
}
int main()
{
    int h, w;
    scanf("%d%d", &h, &w);
    for (int i = 0; i < h; i++) scanf("%d", r + i);
    for (int i = 0; i < w; i++) scanf("%d", c + i);
    for (int i = 0; i < h; i++)
    {
        for (int j = 0; j < r[i]; j++)
            trySet(i, j, 2);
        trySet(i, r[i], 1);
    }
    for (int i = 0; i < w; i++)
    {
        for (int j = 0; j < c[i]; j++)
            trySet(j, i, 2);
        trySet(c[i], i, 1);
    }
    int zcnt = 0;
    for (int i = 0; i < h; i++)
        for (int j = 0; j < w; j++)
            if (a[i][j] == 0) zcnt++;
    long long ans = 1;
    for (int i = 0; i < zcnt; i++) ans = ans * 2 % mod;
    printf("%lld", ans);
    return 0;
}