Problem p03184 (C++)

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

#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define len(s) s.length()
#define forp(i, a, b) for (i = a; i <= b; i++)
#define FOR(tt, a, b) for (int tt = a; tt <= b; tt++)
#define rep(i, n) for (i = 0; i < n; i++)
#define ren(i, n) for (i = n - 1; i >= 0; i--)
#define forn(i, a, b) for (i = a; i >= b; i--)
#define all(v) v.begin(), v.end()
#define b(v) v.begin()
#define e(v) v.end()
#define mem(n, m) memset(n, m, sizeof(n))
#define lb lower_bound
#define ub upper_bound
#define pii pair<int, int>
#define pll pair<long long, long long>
#define vii vector<int>
#define vll vector<long long>
#define gl(cin, s) getline(cin, s);
#define bitc(n) __builtin_popcountll(n)
#define present(s, x) (s.find(x) != s.end())
#define cpresent(s, x) (find(all(s), x) != s.end())
#define tr(container, it)                                                      \
  for (__typeof(container.begin()) it = container.begin();                     \
       it != container.end(); it++)

#define boost ios_base::sync_with_stdio(0)
#define MOD 1000000007
#define EPSILON 1e-9
#define PI 3.14159265358979323846
#define SIZE 200001

typedef long long ll;
typedef unsigned long long ull;
typedef long double ldo;
typedef double db;
using namespace std;
pair<int, int> a[2001];
ll path[2001];
/* Factorial */
long long fact[SIZE];
void f() // Perfect
{
  fact[0] = 1;
  for (long long i = 1; i < SIZE; i++) {
    fact[i] = fact[i - 1] * i;
    fact[i] %= MOD;
  }
}
/* Multiplying 2 Long long Numbers */
inline long long mulmod(long long a, long long b, long long mod) {
  a %= mod;
  long long res = 0LL;
  while (b) {
    if (b % 2) {
      res = (res + a) % mod;
    }
    a = (a * 2) % mod;
    b /= 2;
  }
  return res;
}
/*Function For Calculating Exponent*/
inline long long expo(long long a, long long b, long long mod) {
  a %= mod;
  long long res = 1LL;
  while (b) {
    if (b % 2) {
      // res = mulmod(res,a,mod);
      res = (res * a) % mod;
    }
    // a = mulmod(a,a,mod);
    a = (a * a) % mod;
    b /= 2;
  }
  return res;
}
/*Division */
inline long long mod_div(long long a, long long b, long long mod) {
  return (a * expo(b, mod - 2, mod)) % mod;
}
int main() {
  /* #ifndef ONLINE_JUDGE
  freopen(fi, "r", stdin);
  #endif */
  // freopen("route.in","r",stdin);
  // freopen("route.out","w",stdout);
  // cin.ignore();
  // cin.clear();
  boost;
  // cin.tie(0);
  // cout<<"Case #"<<tt<<": ";
  f();
  int i, n, m, p, x, y, j;
  int T;
  T = 1;
  while (T--) {
    cin >> n >> m >> p;
    swap(n, m);
    forp(i, 1, p) {
      cin >> x >> y;
      swap(x, y);
      a[i] = mp(x, y);
    }
    sort(a + 1, a + 1 + p);
    ll sum = 0;
    forp(i, 1, p) {
      x = a[i].ff;
      y = a[i].ss;
      ll val = mod_div(fact[abs(x - 1) + abs(y - 1)],
                       (fact[abs(x - 1)] * fact[abs(y - 1)]) % MOD, MOD);
      forp(j, 1, i - 1) {
        if (a[j].ff <= x and a[j].ss <= y) {
          ll val2 = mod_div(
              fact[abs(x - a[j].ff) + abs(y - a[j].ss)],
              (fact[abs(x - a[j].ff)] * fact[abs(y - a[j].ss)]) % MOD, MOD);
          val2 = (val2 * path[j]) % MOD;
          val -= val2;
          while (val < 0)
            val += MOD;
        }
      }
      path[i] = val;
      // cout<<val<<endl;
      val = (val * mod_div(fact[abs(x - n) + abs(y - m)],
                           (fact[abs(x - n)] * fact[abs(y - m)]) % MOD, MOD)) %
            MOD;
      sum = (sum + val) % MOD;
    }
    ll FANS = mod_div(fact[n + m - 2], (fact[n - 1] * fact[m - 1]) % MOD, MOD);
    FANS -= sum;
    while (FANS < 0)
      FANS += MOD;
    cout << FANS << endl;
  }
  return 0;
}

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

#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define len(s) s.length()
#define forp(i, a, b) for (i = a; i <= b; i++)
#define FOR(tt, a, b) for (int tt = a; tt <= b; tt++)
#define rep(i, n) for (i = 0; i < n; i++)
#define ren(i, n) for (i = n - 1; i >= 0; i--)
#define forn(i, a, b) for (i = a; i >= b; i--)
#define all(v) v.begin(), v.end()
#define b(v) v.begin()
#define e(v) v.end()
#define mem(n, m) memset(n, m, sizeof(n))
#define lb lower_bound
#define ub upper_bound
#define pii pair<int, int>
#define pll pair<long long, long long>
#define vii vector<int>
#define vll vector<long long>
#define gl(cin, s) getline(cin, s);
#define bitc(n) __builtin_popcountll(n)
#define present(s, x) (s.find(x) != s.end())
#define cpresent(s, x) (find(all(s), x) != s.end())
#define tr(container, it)                                                      \
  for (__typeof(container.begin()) it = container.begin();                     \
       it != container.end(); it++)

#define boost ios_base::sync_with_stdio(0)
#define MOD 1000000007
#define EPSILON 1e-9
#define PI 3.14159265358979323846
#define SIZE 200001

typedef long long ll;
typedef unsigned long long ull;
typedef long double ldo;
typedef double db;
using namespace std;
pair<int, int> a[3001];
ll path[3001];
/* Factorial */
long long fact[SIZE];
void f() // Perfect
{
  fact[0] = 1;
  for (long long i = 1; i < SIZE; i++) {
    fact[i] = fact[i - 1] * i;
    fact[i] %= MOD;
  }
}
/* Multiplying 2 Long long Numbers */
inline long long mulmod(long long a, long long b, long long mod) {
  a %= mod;
  long long res = 0LL;
  while (b) {
    if (b % 2) {
      res = (res + a) % mod;
    }
    a = (a * 2) % mod;
    b /= 2;
  }
  return res;
}
/*Function For Calculating Exponent*/
inline long long expo(long long a, long long b, long long mod) {
  a %= mod;
  long long res = 1LL;
  while (b) {
    if (b % 2) {
      // res = mulmod(res,a,mod);
      res = (res * a) % mod;
    }
    // a = mulmod(a,a,mod);
    a = (a * a) % mod;
    b /= 2;
  }
  return res;
}
/*Division */
inline long long mod_div(long long a, long long b, long long mod) {
  return (a * expo(b, mod - 2, mod)) % mod;
}
int main() {
  /* #ifndef ONLINE_JUDGE
  freopen(fi, "r", stdin);
  #endif */
  // freopen("route.in","r",stdin);
  // freopen("route.out","w",stdout);
  // cin.ignore();
  // cin.clear();
  boost;
  // cin.tie(0);
  // cout<<"Case #"<<tt<<": ";
  f();
  int i, n, m, p, x, y, j;
  int T;
  T = 1;
  while (T--) {
    cin >> n >> m >> p;
    swap(n, m);
    forp(i, 1, p) {
      cin >> x >> y;
      swap(x, y);
      a[i] = mp(x, y);
    }
    sort(a + 1, a + 1 + p);
    ll sum = 0;
    forp(i, 1, p) {
      x = a[i].ff;
      y = a[i].ss;
      ll val = mod_div(fact[abs(x - 1) + abs(y - 1)],
                       (fact[abs(x - 1)] * fact[abs(y - 1)]) % MOD, MOD);
      forp(j, 1, i - 1) {
        if (a[j].ff <= x and a[j].ss <= y) {
          ll val2 = mod_div(
              fact[abs(x - a[j].ff) + abs(y - a[j].ss)],
              (fact[abs(x - a[j].ff)] * fact[abs(y - a[j].ss)]) % MOD, MOD);
          val2 = (val2 * path[j]) % MOD;
          val -= val2;
          while (val < 0)
            val += MOD;
        }
      }
      path[i] = val;
      // cout<<val<<endl;
      val = (val * mod_div(fact[abs(x - n) + abs(y - m)],
                           (fact[abs(x - n)] * fact[abs(y - m)]) % MOD, MOD)) %
            MOD;
      sum = (sum + val) % MOD;
    }
    ll FANS = mod_div(fact[n + m - 2], (fact[n - 1] * fact[m - 1]) % MOD, MOD);
    FANS -= sum;
    while (FANS < 0)
      FANS += MOD;
    cout << FANS << endl;
  }
  return 0;
}