Problem p03234 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mp make_pair
#define fs first
#define sc second
#define pb push_back
#define mod 1000000007
using namespace std;

int n, k;
int match[305];
long long f[305];
long long dp[305][305];
long long dp2[305][305];
long long cnt[305][305];

long long solve(int l, int r) {
  if (dp[l][r] != -1)
    return dp[l][r];
  if ((r - l) % 2 == 0)
    return 0;

  long long ans = 0;
  cnt[l][r] = r - l + 1;

  for (int i = l; i <= r; i++) {
    if (match[i] != -1) {
      int v = match[i];
      if (v >= l && v <= r) {
        cnt[l][r]--;
      } else {
        return dp[l][r] = 0;
      }
    }
  }
  // assert(cnt[l][r] % 2 == 0);

  ans = f[cnt[l][r]];
  return (dp[l][r] = ans);
}

long long solve2(int l, int r) {
  if (dp2[l][r] != -1)
    return dp2[l][r];

  if ((r - l) % 2 == 0)
    return 0;

  long long ans = solve(l, r);
  for (int i = l; i < r; i++) {
    long long ini = (solve2(l, i) * solve(i + 1, r)) % mod;
    ans = (ans + mod - ini) % mod;
  }
  return (dp2[l][r] = ans);
}

int main() {
  cin >> n >> k;

  n *= 2;
  int aktif = n - 2 * k;
  memset(match, -1, sizeof(match));
  for (int i = 1; i <= k; i++) {
    int u, v;
    cin >> u >> v;
    match[u] = v;
    match[v] = u;
  }

  memset(f, 0, sizeof(f));
  f[0] = 1;
  for (int i = 2; i <= n; i++) {
    f[i] = (f[i - 2] * (i - 1)) % mod;
  }

  memset(dp, -1, sizeof(dp));
  memset(dp2, -1, sizeof(dp2));

  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      if ((j + i) % 2 == 0)
        continue;

      solve(i, j);
      long long x = (solve2(i, j) * f[aktif - cnt[i][j]]) % mod;
      ans = (ans + x) % mod;
    }
  }

  cout << ans << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
#define mp make_pair
#define fs first
#define sc second
#define pb push_back
#define mod 1000000007
using namespace std;

int n, k;
int match[610];
long long f[610];
long long dp[610][610];
long long dp2[610][610];
long long cnt[610][610];

long long solve(int l, int r) {
  if (dp[l][r] != -1)
    return dp[l][r];
  if ((r - l) % 2 == 0)
    return 0;

  long long ans = 0;
  cnt[l][r] = r - l + 1;

  for (int i = l; i <= r; i++) {
    if (match[i] != -1) {
      int v = match[i];
      if (v >= l && v <= r) {
        cnt[l][r]--;
      } else {
        return dp[l][r] = 0;
      }
    }
  }
  // assert(cnt[l][r] % 2 == 0);

  ans = f[cnt[l][r]];
  return (dp[l][r] = ans);
}

long long solve2(int l, int r) {
  if (dp2[l][r] != -1)
    return dp2[l][r];

  if ((r - l) % 2 == 0)
    return 0;

  long long ans = solve(l, r);
  for (int i = l; i < r; i++) {
    long long ini = (solve2(l, i) * solve(i + 1, r)) % mod;
    ans = (ans + mod - ini) % mod;
  }
  return (dp2[l][r] = ans);
}

int main() {
  cin >> n >> k;

  n *= 2;
  int aktif = n - 2 * k;
  memset(match, -1, sizeof(match));
  for (int i = 1; i <= k; i++) {
    int u, v;
    cin >> u >> v;
    match[u] = v;
    match[v] = u;
  }

  memset(f, 0, sizeof(f));
  f[0] = 1;
  for (int i = 2; i <= n; i++) {
    f[i] = (f[i - 2] * (i - 1)) % mod;
  }

  memset(dp, -1, sizeof(dp));
  memset(dp2, -1, sizeof(dp2));

  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      if ((j + i) % 2 == 0)
        continue;

      solve(i, j);
      long long x = (solve2(i, j) * f[aktif - cnt[i][j]]) % mod;
      ans = (ans + x) % mod;
    }
  }

  cout << ans << "\n";
}