Problem p03152 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <unordered_map>
#include <unordered_set>
#endif

int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }

#define ll long long
#define pb push_back
#define ld long double
#define mp make_pair
#define F first
#define S second
#define pii pair<ll, ll>

using namespace ::std;

const ll maxn = 2e5 + 500;
const ll mod = 1e9 + 7;

ll zarb(long long a, long long b) { return (a * b) % mod; }
ll jam(long long a, long long b) { return (a + b) % mod; }
ll fac[maxn];
ll rfac[maxn];

ll poww(ll a, ll b) {
  ll ans = 1;
  while (b) {
    if (b & 1) {
      ans = zarb(ans, a);
    }
    a = zarb(a, a);
    b >>= 1;
  }
  return ans;
}
ll ent(ll n, ll k) {
  if (k > n || k < 0)
    return 0;
  return zarb(fac[n], zarb(rfac[k], rfac[n - k]));
}
ll juy(ll n, ll k) {
  if (k > n || k < 0)
    return 0;
  return zarb(fac[n], rfac[n - k]);
}

ll a[maxn];
ll b[maxn];
ll n, m;

ll findAns(ll x, ll y, ll c) {
  ll ax = a[x] - c;
  ll by = b[y] - c;
  ll tolx = n - x;
  ll toly = m - y;
  ll s = tolx * toly;
  if (s == 0)
    return 1;
  if (ax == by) {
    return zarb(findAns(x + 1, y + 1, c + tolx + toly - 1),
                juy(ax - 1, tolx + toly - 2));
  }
  if (ax < by) {
    return zarb(zarb(findAns(x + 1, y, c + toly), juy(ax - 1, toly - 1)), toly);
  }
  if (ax > by) {
    return zarb(zarb(findAns(x, y + 1, c + tolx), juy(by - 1, tolx - 1)), tolx);
  }
}
int main() {
  fac[0] = 1;
  for (ll i = 1; i < maxn; i++) {
    fac[i] = zarb(fac[i - 1], i);
  }
  for (ll i = 0; i < maxn; i++) {
    rfac[i] = poww(fac[i], mod - 2);
  }

  cin >> n >> m;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (ll i = 0; i < m; i++) {
    cin >> b[i];
  }
  sort(a, a + n);
  sort(b, b + m);
  for (ll i = 0; i + 1 < n; i++) {
    if (a[i] == a[i + 1]) {
      cout << 0;
      return 0;
    }
  }
  for (ll i = 0; i + 1 < m; i++) {
    if (b[i] == b[i + 1]) {
      cout << 0;
      return 0;
    }
  }

  cout << findAns(0, 0, 0);
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <unordered_map>
#include <unordered_set>
#endif

int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }

#define ll long long
#define pb push_back
#define ld long double
#define mp make_pair
#define F first
#define S second
#define pii pair<ll, ll>

using namespace ::std;

const ll maxn = 1e6 + 500;
const ll mod = 1e9 + 7;

ll zarb(long long a, long long b) { return (a * b) % mod; }
ll jam(long long a, long long b) { return (a + b) % mod; }
ll fac[maxn];
ll rfac[maxn];

ll poww(ll a, ll b) {
  ll ans = 1;
  while (b) {
    if (b & 1) {
      ans = zarb(ans, a);
    }
    a = zarb(a, a);
    b >>= 1;
  }
  return ans;
}
ll ent(ll n, ll k) {
  if (k > n || k < 0)
    return 0;
  return zarb(fac[n], zarb(rfac[k], rfac[n - k]));
}
ll juy(ll n, ll k) {
  if (k > n || k < 0)
    return 0;
  return zarb(fac[n], rfac[n - k]);
}

ll a[maxn];
ll b[maxn];
ll n, m;

ll findAns(ll x, ll y, ll c) {
  ll ax = a[x] - c;
  ll by = b[y] - c;
  ll tolx = n - x;
  ll toly = m - y;
  ll s = tolx * toly;
  if (s == 0)
    return 1;
  if (ax == by) {
    return zarb(findAns(x + 1, y + 1, c + tolx + toly - 1),
                juy(ax - 1, tolx + toly - 2));
  }
  if (ax < by) {
    return zarb(zarb(findAns(x + 1, y, c + toly), juy(ax - 1, toly - 1)), toly);
  }
  if (ax > by) {
    return zarb(zarb(findAns(x, y + 1, c + tolx), juy(by - 1, tolx - 1)), tolx);
  }
}
int main() {
  fac[0] = 1;
  for (ll i = 1; i < maxn; i++) {
    fac[i] = zarb(fac[i - 1], i);
  }
  for (ll i = 0; i < maxn; i++) {
    rfac[i] = poww(fac[i], mod - 2);
  }

  cin >> n >> m;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (ll i = 0; i < m; i++) {
    cin >> b[i];
  }
  sort(a, a + n);
  sort(b, b + m);
  for (ll i = 0; i + 1 < n; i++) {
    if (a[i] == a[i + 1]) {
      cout << 0;
      return 0;
    }
  }
  for (ll i = 0; i + 1 < m; i++) {
    if (b[i] == b[i + 1]) {
      cout << 0;
      return 0;
    }
  }

  cout << findAns(0, 0, 0);
}