Problem p03152 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

// #define int long long

#define pii pair<int, int>

#define x1 x1228
#define y1 y1228

#define left left228
#define right right228

#define pb push_back
#define eb emplace_back

#define mp make_pair

#define ff first
#define ss second

#define matr vector<vector<int>>

#define all(x) x.begin(), x.end()

using namespace std;
typedef long long ll;
typedef long double ld;

const int maxn = 3e5 + 7, mod = 1e9 + 7, inf = 1e9, MAXN = 1e6 + 7;
const double eps = 1e-9;
mt19937 rnd(time(0));
int n, m;
int A[maxn];
int B[maxn];
int a[maxn], b[maxn];

void solve() {
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    a[i] = x;
    if (A[x]) {
      cout << 0;
      return;
    }
    A[x] = 1;
  }
  for (int i = 0; i < m; ++i) {
    int x;
    cin >> x;
    b[i] = x;
    if (B[x]) {
      cout << 0;
      return;
    }
    B[x] = 1;
  }
  ll ans = 1;
  int have = -1;
  for (int i = n * m; i > 0; --i) {
    have++;
    if (A[i] && B[i]) {
      continue;
    }
    if (A[i]) {
      int cur = 0;
      for (int j = 0; j < m; ++j) {
        if (b[j] >= i) {
          ++cur;
        }
      }
      ans *= cur;
      ans %= mod;
      continue;
    }
    if (B[i]) {
      int cur = 0;
      for (int j = 0; j < n; ++j) {
        if (a[j] >= i) {
          ++cur;
        }
      }
      ans *= cur;
      ans %= mod;
      continue;
    }
    int p1 = 0;
    int p2 = 0;
    for (int j = 0; j < n; ++j) {
      p1 += (a[j] >= i);
    }
    for (int j = 0; j < m; ++j) {
      p2 += (b[j] >= i);
    }
    int d = p1 * p2;

    d -= have;
    ans *= d;
    ans %= mod;
  }
  cout << ans;
}

signed main() {
#ifdef LOCAL
  freopen("TASK.in", "r", stdin);
  freopen("TASK.out", "w", stdout);
#else

#endif // LOCAL
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(20);
  cout << fixed;
  int t = 1;
  for (int i = 0; i < t; ++i)
    solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

// #define int long long

#define pii pair<int, int>

#define x1 x1228
#define y1 y1228

#define left left228
#define right right228

#define pb push_back
#define eb emplace_back

#define mp make_pair

#define ff first
#define ss second

#define matr vector<vector<int>>

#define all(x) x.begin(), x.end()

using namespace std;
typedef long long ll;
typedef long double ld;

const int maxn = 1e6 + 7, mod = 1e9 + 7, inf = 1e9, MAXN = 1e6 + 7;
const double eps = 1e-9;
mt19937 rnd(time(0));
int n, m;
int A[maxn];
int B[maxn];
int a[maxn], b[maxn];

void solve() {
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    a[i] = x;
    if (A[x]) {
      cout << 0;
      return;
    }
    A[x] = 1;
  }
  for (int i = 0; i < m; ++i) {
    int x;
    cin >> x;
    b[i] = x;
    if (B[x]) {
      cout << 0;
      return;
    }
    B[x] = 1;
  }
  ll ans = 1;
  int have = -1;
  for (int i = n * m; i > 0; --i) {
    have++;
    if (A[i] && B[i]) {
      continue;
    }
    if (A[i]) {
      int cur = 0;
      for (int j = 0; j < m; ++j) {
        if (b[j] >= i) {
          ++cur;
        }
      }
      ans *= cur;
      ans %= mod;
      continue;
    }
    if (B[i]) {
      int cur = 0;
      for (int j = 0; j < n; ++j) {
        if (a[j] >= i) {
          ++cur;
        }
      }
      ans *= cur;
      ans %= mod;
      continue;
    }
    int p1 = 0;
    int p2 = 0;
    for (int j = 0; j < n; ++j) {
      p1 += (a[j] >= i);
    }
    for (int j = 0; j < m; ++j) {
      p2 += (b[j] >= i);
    }
    int d = p1 * p2;

    d -= have;
    ans *= d;
    ans %= mod;
  }
  cout << ans;
}

signed main() {
#ifdef LOCAL
  freopen("TASK.in", "r", stdin);
  freopen("TASK.out", "w", stdout);
#else

#endif // LOCAL
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(20);
  cout << fixed;
  int t = 1;
  for (int i = 0; i < t; ++i)
    solve();
  return 0;
}