Problem p03152 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define MAX_N 200010
long long inv[MAX_N];
long long factorial[MAX_N];
long long inv_factorial[MAX_N];

void getInv() {
  for (int i = 1; i < MAX_N; i++) {
    if (i == 1)
      inv[i] = 1;
    else {
      inv[i] = (mod - (mod / i) * inv[mod % i]) % mod;
      if (inv[i] < 0)
        inv[i] += mod;
    }
  }
}
void getFactorial() {
  factorial[0] = 1;
  inv_factorial[0] = 1;
  for (int i = 1; i < MAX_N; i++) {
    factorial[i] = factorial[i - 1] * i;
    factorial[i] %= mod;
    inv_factorial[i] = inv_factorial[i - 1] * inv[i];
    inv_factorial[i] %= mod;
  }
}

long long cal(int n, int r) {
  long long ret = factorial[n] * inv_factorial[n - r];
  return ret % mod;
}

int main() {
  int n, m;
  cin >> n >> m;
  long long ans = 1;
  getInv();
  getFactorial();
  vector<int> a(n), b(m);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < m; i++)
    cin >> b[i];
  sort(a.rbegin(), a.rend());
  sort(b.rbegin(), b.rend());
  for (int i = 0; i < n - 1; i++) {
    if (a[i] == a[i + 1]) {
      cout << 0 << endl;
      return 0;
    }
  }
  for (int i = 0; i < m - 1; i++) {
    if (b[i] == b[i + 1]) {
      cout << 0 << endl;
      return 0;
    }
  }
  /*
  vector< vector<int> > v(n, vector<int>(m, 0));
  int p = 0; int q = 0;
  while (p < n && q < m) {
      if (a[p] == b[q]) v[p][q] = a[p];
      if (a[p] > b[q]) p++;
      if (a[p] < b[q]) q++;
  }
  */
  map<int, int> used;
  vector<pair<int, int>> u = {{0, 0}};
  map<int, int> ma;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i] == b[j]) {
        used[a[i]]++;
        continue;
      }
      ma[min(a[i], b[j])]++;
    }
  }
  for (auto it = ma.begin(); it != ma.end(); it++) {
    u.push_back({it->first, it->second});
  }
  sort(u.rbegin(), u.rend());
  int mm = n * m;
  int rest = 0;
  int count = 0;
  for (int i = 0; i < u.size() - 1; i++) {
    int p = u[i].first;
    int q = u[i].second;
    int np = u[i + 1].first;
    for (int k = mm; k > p; k--) {
      if (used[k] == 0) {
        cout << 0 << endl;
        return 0;
      }
    }
    mm = p;
    rest += q;
    int count = 0;
    for (int k = mm; k > np; k--) {
      if (used[k] == 0) {
        count++;
      }
    }
    if (used[p] == 1) {
      ans = ans * cal(rest, count) % mod;
    } else {
      ans = (ans * cal(rest - 1, count - 1) % mod) * q % mod;
    }
    rest -= count;
    mm = np;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define MAX_N 1000010
long long inv[MAX_N];
long long factorial[MAX_N];
long long inv_factorial[MAX_N];

void getInv() {
  for (int i = 1; i < MAX_N; i++) {
    if (i == 1)
      inv[i] = 1;
    else {
      inv[i] = (mod - (mod / i) * inv[mod % i]) % mod;
      if (inv[i] < 0)
        inv[i] += mod;
    }
  }
}
void getFactorial() {
  factorial[0] = 1;
  inv_factorial[0] = 1;
  for (int i = 1; i < MAX_N; i++) {
    factorial[i] = factorial[i - 1] * i;
    factorial[i] %= mod;
    inv_factorial[i] = inv_factorial[i - 1] * inv[i];
    inv_factorial[i] %= mod;
  }
}

long long cal(int n, int r) {
  long long ret = factorial[n] * inv_factorial[n - r];
  return ret % mod;
}

int main() {
  int n, m;
  cin >> n >> m;
  long long ans = 1;
  getInv();
  getFactorial();
  vector<int> a(n), b(m);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < m; i++)
    cin >> b[i];
  sort(a.rbegin(), a.rend());
  sort(b.rbegin(), b.rend());
  for (int i = 0; i < n - 1; i++) {
    if (a[i] == a[i + 1]) {
      cout << 0 << endl;
      return 0;
    }
  }
  for (int i = 0; i < m - 1; i++) {
    if (b[i] == b[i + 1]) {
      cout << 0 << endl;
      return 0;
    }
  }
  /*
  vector< vector<int> > v(n, vector<int>(m, 0));
  int p = 0; int q = 0;
  while (p < n && q < m) {
      if (a[p] == b[q]) v[p][q] = a[p];
      if (a[p] > b[q]) p++;
      if (a[p] < b[q]) q++;
  }
  */
  map<int, int> used;
  vector<pair<int, int>> u = {{0, 0}};
  map<int, int> ma;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i] == b[j]) {
        used[a[i]]++;
        continue;
      }
      ma[min(a[i], b[j])]++;
    }
  }
  for (auto it = ma.begin(); it != ma.end(); it++) {
    u.push_back({it->first, it->second});
  }
  sort(u.rbegin(), u.rend());
  int mm = n * m;
  int rest = 0;
  int count = 0;
  for (int i = 0; i < u.size() - 1; i++) {
    int p = u[i].first;
    int q = u[i].second;
    int np = u[i + 1].first;
    for (int k = mm; k > p; k--) {
      if (used[k] == 0) {
        cout << 0 << endl;
        return 0;
      }
    }
    mm = p;
    rest += q;
    int count = 0;
    for (int k = mm; k > np; k--) {
      if (used[k] == 0) {
        count++;
      }
    }
    if (used[p] == 1) {
      ans = ans * cal(rest, count) % mod;
    } else {
      ans = (ans * cal(rest - 1, count - 1) % mod) * q % mod;
    }
    rest -= count;
    mm = np;
  }
  cout << ans << endl;
  return 0;
}