Problem p03152 (C++)

Buggy submission (status: Runtime Error):
// :)
// "Khodaya, be man "Tagwaye setiz" biamooz ta
//  dar anbuh masuliat nalaghzam ..." -Shariati
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define F first
#define S second
#define MP make_pair
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag,
                              tree_order_statistics_node_update>;
typedef long long int64;
typedef pair<int, int> pii;
typedef pair<int64, int64> pii64;
const int maxn = 1e3 + 9;
const int64 M = 1e9 + 7;
const int64 I = 1e9;

pii p[maxn];
int a[maxn], b[maxn], mx1[maxn], mx2[maxn];
bool vis1[maxn], vis2[maxn];
int n, m, ans = 1;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    p[a[i]].F = i;
  }
  for (int i = 1; i <= m; i++) {
    cin >> b[i];
    p[b[i]].S = i;
  }
  int c1 = 0, c2 = 0, num = 0;
  for (int i = n * m; i >= 1; i--) {
    if (p[i].F && p[i].S) {
      if (vis1[p[i].F]) {
        ans = 0;
        continue;
      }
      if (vis2[p[i].S]) {
        ans = 0;
        continue;
      }
      vis1[p[i].F] = vis2[p[i].S] = true;
      c1++;
      c2++;
      continue;
    }
    if (p[i].F) {
      if (vis1[p[i].F]) {
        ans = 0;
        continue;
      }
      vis1[p[i].F] = true;
      ans = 1LL * ans * c2 % M;
      c1++;
      continue;
    }
    if (p[i].S) {
      if (vis2[p[i].S]) {
        ans = 0;
        continue;
      }
      vis2[p[i].S] = true;
      ans = 1LL * ans * c1 % M;
      c2++;
      continue;
    }
    ans = 1LL * ans * (c1 * c2 - (n * m - i)) % M;
  }
  cout << ans << "\n";
}

Fixed submission:
// :)
// "Khodaya, be man "Tagwaye setiz" biamooz ta
//  dar anbuh masuliat nalaghzam ..." -Shariati
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define F first
#define S second
#define MP make_pair
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag,
                              tree_order_statistics_node_update>;
typedef long long int64;
typedef pair<int, int> pii;
typedef pair<int64, int64> pii64;
const int maxn = 1e3 + 9;
const int64 M = 1e9 + 7;
const int64 I = 1e9;

pii p[maxn * maxn];
int a[maxn], b[maxn], mx1[maxn], mx2[maxn];
bool vis1[maxn], vis2[maxn];
int n, m, ans = 1;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    p[a[i]].F = i;
  }
  for (int i = 1; i <= m; i++) {
    cin >> b[i];
    p[b[i]].S = i;
  }
  int c1 = 0, c2 = 0, num = 0;
  for (int i = n * m; i >= 1; i--) {
    if (p[i].F && p[i].S) {
      if (vis1[p[i].F]) {
        ans = 0;
        continue;
      }
      if (vis2[p[i].S]) {
        ans = 0;
        continue;
      }
      vis1[p[i].F] = vis2[p[i].S] = true;
      c1++;
      c2++;
      continue;
    }
    if (p[i].F) {
      if (vis1[p[i].F]) {
        ans = 0;
        continue;
      }
      vis1[p[i].F] = true;
      ans = 1LL * ans * c2 % M;
      c1++;
      continue;
    }
    if (p[i].S) {
      if (vis2[p[i].S]) {
        ans = 0;
        continue;
      }
      vis2[p[i].S] = true;
      ans = 1LL * ans * c1 % M;
      c2++;
      continue;
    }
    ans = 1LL * ans * (c1 * c2 - (n * m - i)) % M;
  }
  cout << ans << "\n";
}