#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define BIT(X, i) ((X>>i)&1)
#define REP(i, a, b) for(int i = (a); i < (b); ++i)
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
#define r_all(a) (a).rbegin(), (a).rend()
#define in(x) cin>>x
#define ina(x, n) rep(x_i,n) cin>>x[x_i]
#define outa(x, n) rep(x_i,n) cout<<x[x_i]<<"\t\n"[x_i==n-1]

typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> pll;
typedef vector<pll> vll;
typedef priority_queue<ll> pq;

const int MOD = 1e9+7;
const int INF = (1<<30);
const ll INFL = (1L<<62);

int main() {
  ll n, m; cin >> n >> m;
  vl a(n), b(m); ina(a,n); ina(b,m);
  sort(all(a)); sort(all(b));
  rep(i,n-1) if (a[i]==a[i+1]) { puts("0"); return 0; }
  rep(i,m-1) if (b[i]==b[i+1]) { puts("0"); return 0; }
  ll ans = 1;
  for (ll now = n*m; now > 0; now--) {
    auto ita = lower_bound(all(a), now);
    auto itb = lower_bound(all(b), now);
    int st = (*ita == now) + (*itb == now);
    if (st == 0) {
      ans *= (a.end() - ita)*(b.end() - itb) - (n*m - now);
    }
    if (st == 1 && *ita == now) {
      ans *= (b.end() - itb);
    }
    if (st == 1 && *itb == now) {
      ans *= (a.end() - ita);
    }
    ans %= MOD;
  }
  cout << ans << endl;
  return 0;
}

#include <bits/stdc++.h>
#define REP(i, n) for(long long i=0; i<n; i++)
#define REPR(i, n) for(long long i=n-1; i>=0; i--)
#define FOR(i, m, n) for(long long i=m; i<=n; i++)
#define FORR(i, m, n) for(long long i=m; i>=n; i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define VSORTR(v) sort(v.rbegin(), v.rend());
#define ALL(v) (v).begin(),(v).end()
#define FIN ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;
const ll mod = 1e9+7;
const ll inf = 1e15;



int main(){FIN

    ll n,m;cin>>n>>m;
    vll a(n);
    REP(i,n) cin>>a[i];
    vll rui(n+1,0);
    ll sum=0;
    REP(i,n){
        sum += a[i];
        rui[i+1] = sum % m; 
    }
    map<ll,ll> mp;
    REP(i,n+1) mp[rui[i]]++;
    ll ans=0;
    for(auto p:mp){
        // cout<<p.second<<endl;
        ans += p.second * (p.second-1) /2;
    } 
    cout<<ans<<endl;
    return 0;
}