Problem p03234 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
#define pb push_back
#define pii pair<int, int>
#define F first
#define S second
#define endl '\n'
#define int long long
#define sync                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#define kill(x) return cout << x << '\n', 0;
using namespace std;
const int N = 3e2 + 10, mod = 1e9 + 7;
ll dp[N][N];
ll pd[N][N];
ll t[N][N];
ll nxt[N];
int32_t main() {
  ll n, m;
  cin >> n >> m;
  n *= 2;
  for (int i = 1; i <= m; i++) {
    ll u, v;
    cin >> u >> v;
    nxt[u] = v;
    nxt[v] = u;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n + 1; j++) {
      if ((j - i) % 2 == 1)
        continue;
      ll p1 = 0;
      ll ts = 0;
      for (int k = i; k < j; k++) {
        if (nxt[k] != 0) {
          if (nxt[k] < i || nxt[k] >= j)
            p1 = 1;
        } else
          ts++;
      }
      if (p1)
        continue;
      ll ans = 1;
      ll z = ts;
      z--;
      while (z > 0) {
        ans *= z;
        z -= 2;
        ans %= mod;
      }
      pd[i][j] = ans;
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n + 1; j++) {
      if ((j - i) % 2 == 1)
        continue;
      ll p1 = 0;
      ll ts = 0;
      for (int k = i; k < j; k++) {
        if (nxt[k] != 0) {
          if (nxt[k] < i && nxt[k] >= j)
            p1 = 1;
        }
      }
      if (p1)
        continue;
      for (int k = 1; k < i; k++)
        if (nxt[k] == 0)
          ts++;
      for (int k = j; k <= n; k++) {
        if (nxt[k] == 0)
          ts++;
      }
      ll ans = 1;
      ll z = ts;
      z--;
      while (z > 0) {
        ans *= z;
        z -= 2;
        ans %= mod;
      }
      t[i][j] = ans;
    }
  }
  for (int i = n - 1; i > 0; i--) {
    for (int j = i + 2; j <= n + 1; j += 2) {
      ll p1 = 0;
      ll ts = 0;
      for (int k = i; k < j; k++) {
        if (nxt[k] != 0) {
          if (nxt[k] < i || nxt[k] >= j)
            p1 = 1;
        } else
          ts++;
      }
      if (p1)
        continue;
      dp[i][j] = pd[i][j];
      for (int k = i + 1; k < j - 1; k++) {
        dp[i][j] -= dp[i][k] * pd[k][j] % mod;
        dp[i][j] += mod;
        dp[i][j] %= mod;
      }
      // cout << i << " " << j << " " << dp[i][j] << " " << pd[i][j] << endl;
    }
  }
  ll ans = 0;
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      ans += dp[i][j] * t[i][j] % mod;
  cout << ans % mod << endl;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
#define pb push_back
#define pii pair<int, int>
#define F first
#define S second
#define endl '\n'
#define int long long
#define sync                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#define kill(x) return cout << x << '\n', 0;
using namespace std;
const int N = 6e2 + 10, mod = 1e9 + 7;
ll dp[N][N];
ll pd[N][N];
ll t[N][N];
ll nxt[N];
int32_t main() {
  ll n, m;
  cin >> n >> m;
  n *= 2;
  for (int i = 1; i <= m; i++) {
    ll u, v;
    cin >> u >> v;
    nxt[u] = v;
    nxt[v] = u;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n + 1; j++) {
      if ((j - i) % 2 == 1)
        continue;
      ll p1 = 0;
      ll ts = 0;
      for (int k = i; k < j; k++) {
        if (nxt[k] != 0) {
          if (nxt[k] < i || nxt[k] >= j)
            p1 = 1;
        } else
          ts++;
      }
      if (p1)
        continue;
      ll ans = 1;
      ll z = ts;
      z--;
      while (z > 0) {
        ans *= z;
        z -= 2;
        ans %= mod;
      }
      pd[i][j] = ans;
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n + 1; j++) {
      if ((j - i) % 2 == 1)
        continue;
      ll p1 = 0;
      ll ts = 0;
      for (int k = i; k < j; k++) {
        if (nxt[k] != 0) {
          if (nxt[k] < i && nxt[k] >= j)
            p1 = 1;
        }
      }
      if (p1)
        continue;
      for (int k = 1; k < i; k++)
        if (nxt[k] == 0)
          ts++;
      for (int k = j; k <= n; k++) {
        if (nxt[k] == 0)
          ts++;
      }
      ll ans = 1;
      ll z = ts;
      z--;
      while (z > 0) {
        ans *= z;
        z -= 2;
        ans %= mod;
      }
      t[i][j] = ans;
    }
  }
  for (int i = n - 1; i > 0; i--) {
    for (int j = i + 2; j <= n + 1; j += 2) {
      ll p1 = 0;
      ll ts = 0;
      for (int k = i; k < j; k++) {
        if (nxt[k] != 0) {
          if (nxt[k] < i || nxt[k] >= j)
            p1 = 1;
        } else
          ts++;
      }
      if (p1)
        continue;
      dp[i][j] = pd[i][j];
      for (int k = i + 1; k < j - 1; k++) {
        dp[i][j] -= dp[i][k] * pd[k][j] % mod;
        dp[i][j] += mod;
        dp[i][j] %= mod;
      }
      // cout << i << " " << j << " " << dp[i][j] << " " << pd[i][j] << endl;
    }
  }
  ll ans = 0;
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      ans += dp[i][j] * t[i][j] % mod;
  cout << ans % mod << endl;
}