h, w = [int(z) for z in input().split()]
r = [int(z) for z in input().split()]
c = [int(z) for z in input().split()]
mod = 10**9 + 7
grid = [['?' for col in range(w+1)] for row in range(h+1)]
def try_set(row, col, target):
if grid[row][col] == '?':
grid[row][col] = target
elif grid[row][col] != target:
raise ValueError
def go():
try:
for row in range(h):
for col in range(r[row]):
try_set(row, col, 'FULL')
try_set(row, r[row], 'EMPTY')
for col in range(w):
for row in range(c[col]):
try_set(row, col, 'FULL')
try_set(c[col], col, 'EMPTY')
except ValueError:
return 0
answer = 1
for row in range(h):
for col in range(w):
if grid[row][col] == '?': answer = answer * 2 % mod
return answer
print(go())
// NGUYEN QUOC HUNG - quochung.cyou
#include <bits/stdc++.h>
using namespace std;
// Template
template <typename T>
istream &operator>>(istream &in, vector<T> &a) {
for (auto &x : a)
in >> x;
return in;
};
template <typename T>
ostream &operator<<(ostream &out, vector<T> &a) {
for (auto &x : a)
out << x << ' ';
return out;
};
template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &x) { return out << x.f << ' ' << x.s; }
template <typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &x) { return in >> x.f >> x.s; }
template <typename T>
using matrix = vector<vector<T>>;
template <typename T>
using rubik = vector<vector<vector<T>>>;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef long long ll;
typedef long double ld;
#define db(x) cout << (x) << '\n';
#define faster() \
ios_base::sync_with_stdio(0); \
cin.tie(NULL); \
cout.tie(NULL);
#define FORAB(i, a, b) for (int i = a; i < (b); i++)
#define FOR(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, a, b) for (int i = (b)-1; i >= a; i--)
#define ull unsigned long long int
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert
const int MOD = 1000000007;
ll powMod(ll a, ll b, ll mod = MOD) {
ll res = 1;
a %= mod;
for (; b; b >>= 1) {
if (b & 1)
res = res * a % mod;
a = a * a % mod;
}
return res;
}
int main() {
faster();
int h, w;
cin >> h >> w;
int a[h * 2 + 5][w * 2 + 5] = {};
int yeuban[h * 2 + 5][w * 2 + 5] = {};
int hang[h + 5];
int cot[w + 5];
FOR(i, h) {
int x;
cin >> x;
hang[i] = x;
FOR(j, x) {
a[i][j] = 1;
}
yeuban[i][x] = 2;
// a[i][x+1] = 1;
}
FOR(i, w) {
int x;
cin >> x;
cot[i] = x;
FOR(j, x) {
a[j][i] = 1;
}
yeuban[x][i] = 2;
// a[x+1][i] = 1;
}
FOR(i, h) {
FOR(j, w) {
if (yeuban[i][j] && a[i][j]) {
cout << 0 << "\n";
return 0;
}
if (yeuban[i][j] || a[i][j]) {
a[i][j] = 1;
}
}
}
int dem = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
// cout << a[i][j] << " ";
if (a[i][j] == 0) {
dem++;
}
}
cout << endl;
}
ll kq = powMod(2, dem);
cout << kq;
}
977A - Wrong Subtraction | 263A - Beautiful Matrix |
180C - Letter | 151A - Soft Drinking |
1352A - Sum of Round Numbers | 281A - Word Capitalization |
1646A - Square Counting | 266A - Stones on the Table |
61A - Ultra-Fast Mathematician | 148A - Insomnia cure |
1650A - Deletions of Two Adjacent Letters | 1512A - Spy Detected |
282A - Bit++ | 69A - Young Physicist |
1651A - Playoff | 734A - Anton and Danik |
1300B - Assigning to Classes | 1647A - Madoka and Math Dad |
710A - King Moves | 1131A - Sea Battle |
118A - String Task | 236A - Boy or Girl |
271A - Beautiful Year | 520B - Two Buttons |
231A - Team | 479C - Exams |
1030A - In Search of an Easy Problem | 158A - Next Round |
71A - Way Too Long Words | 160A - Twins |