#include <bits/stdc++.h>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <iostream>
#include <bitset>
#include <cassert>
#include <queue>
#include <random>
#include <stack>
#include <iomanip>
#include <math.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)n; i++)
#define repf(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define repr(i, a, b) for (ll i = (ll)a; i > (ll)b; i--)
#define repv(x, arr) for (auto &x : arr)
#define all(v) (v).begin(), (v).end()
#define vec(name, num) vector<ll> name((num), 0);
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<int> vii;
typedef vector<vii> vvii;
const ll mod = 1e9 + 7;
const int infi = 2147483600;
const ll infl = 1e17;

int n,m;



int main()
{
    cin >> n >> m;
    vii a(n), b(m);
    rep(i, n) cin >> a[i];
    rep(i, m) cin >> b[i];
    sort(all(a));
    sort(all(b));
    rep(i, n - 1)
    {
        if (a[i] == a[i + 1])
        {
            cout << 0 << endl;
            return 0;
        }
    }
    rep(i, m - 1)
    {
        if (b[i] == b[i + 1])
        {
            cout << 0 << endl;
            return 0;
        }
    }
    ll ans = 1;
    repr(now,n*m,0){
        int aidx = lower_bound(all(a), now) - a.begin();
        int bidx = lower_bound(all(b), now) - b.begin();
        ll atar = n - aidx, btar = m - bidx;
        ll kimeta = n * m  - now;
        if (a[aidx]==now && b[bidx]==now){
            continue;
        }
        else if(a[aidx]==now){
            ans = (ans * btar) % mod;
        }
        else if(b[bidx]==now){
            ans = (ans * atar) % mod;
        }
        else {
            ans = (ans * ((atar * btar) % mod + mod - kimeta) % mod) % mod;
        }
    }
    cout << ans << endl;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rev(i, n) for(int i = (int)(n - 1); i >= 0; i--)
#define rev1(i, n) for(int i = (int)(n); i > 0; i--)
#define pb push_back
#define all(v) ((v).begin(), (v).end())
using ll = long long;
using P = pair<int, int>;
/* ------------------------------------------------ */
ll fact(int i) {       //階乗
    if (i == 0) return 1;
    return (fact(i - 1)) * i;
}
ll gcd(ll a, ll b) {        //最大公約数
    if(b == 0) return a;
    return gcd(b, a % b); 
}
ll lcm(ll a, ll b) {      //最小公倍数
    return a * b / gcd(a, b);
}
int keta(ll n) {        //桁数を求める
    if(n == 0) return 1;
    int count = 0;
    while(n != 0) {
        n /= 10;
        count++;
    }
    return count;
}
ll ketasum(ll n) {    //各桁の和
    ll sum = 0;
    while(n != 0) {
        sum += n % 10;
        n /= 10;
    }
    return sum;
}
/* ------------------------------------------------ */
int main() {
    int h,w;
    cin >> h >> w;
    char s[55][55];
    rep(i, h) {
        rep(j, w) cin >> s[i][j];
    }
    int dist[55][55];
    rep(i, h) {
        rep(j, w) dist[i][j] = -1;
    }
    dist[0][0] = 1;
    int f[]={0,1,0,-1};
    int g[]={1,0,-1,0};
    queue<P> que;
    que.push({0,0});
    while(!que.empty()) {
        P p = que.front();
        que.pop();
        rep(i, 4) {
            int x = p.first + f[i],y = p.second + g[i];
            if(x > h-1 || x < 0 || y > w-1 || y < 0) continue;
            if(dist[x][y] == -1 && s[x][y] == '.'){
                dist[x][y] = dist[p.first][p.second]+1;
                que.push({x,y});
            }
        }
    }
    if(dist[h-1][w-1]==-1) {
        cout << -1 << endl;
        return 0;
    }
    int ans = h*w - dist[h - 1][w - 1];
    rep(i, h) {
        rep(j, w) if(s[i][j] == '#') ans--;
    }
    cout << ans << endl;
    return 0;
}