#include<bits/stdc++.h>
#define ll long long int
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
static const ll MAX = 1000000000000000;
static const int NIL = -1;
using namespace std;
const long double EPS = 0.0000000001;
const long double PI = (acos(-1));
const int MOD = 1000000007;

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")


int main() {

    ll n,a; cin >> n;
    map<ll,ll> mp;
    set<ll> st;
    ll ans = 1;
    ll prev,val;
    bool f = false;

    rep(i,n){
        cin >> a;
        if(mp.count(a) == 0) mp[a] = 1;
        else mp[a] ++;
        st.insert(a);

        if(a == 0 && i != 0){
            cout << 0;
            return 0;
        }
        if(a == 0 && i == 0) f = true;
    }

    if(!f){
        cout << 0;
        return 0;
    }



    for(auto itr = st.begin(); itr != st.end(); ++itr) {
        if(*itr == 0) {
            prev = mp[*itr];
            val = *itr;
        }
        else{
            if(*itr - val != 1){
                cout << 0;
                return 0;
            }
            if(prev == 0){
                cout << 0;
                return 0;
            }
            rep(i,mp[*itr]){
                ans = (prev*ans)%998244353;
            }
            prev = mp[*itr];
            val = *itr;
        }
    }
    cout << ans;


    return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e3+10;
const LL MOD=1e9+7;
int n,m,px[N*N],py[N*N];

int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++) {
		int x; scanf("%d",&x);
		if (px[x]) { puts("0"); return 0; }
		px[x]=1;
	}
	for (int j=1;j<=m;j++) {
		int y; scanf("%d",&y);
		if (py[y]) { puts("0"); return 0; }
		py[y]=1;
	}
	
	LL ans=1,usedN=0,usedM=0;
	for (LL i=n*m;i;i--) {
		if (px[i] && py[i]) { usedN++; usedM++; continue; }
		if (px[i]) { usedN++; ans=(ans*usedM)%MOD; }
		if (py[i]) { usedM++; ans=(ans*usedN)%MOD; }
		if (!px[i] && !py[i]) ans=(ans*(usedN*usedM-(n*m-i)))%MOD;
	}
	cout<<ans;
	return 0;
}