Problem p03234 (C++)

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

using ll = long long;
const ll MOD = 1e9 + 7;

ll dpL[301][301];
ll dpM[301][301];
ll dpR[301][301];

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(k), b(k);
  for (int i = 0; i < k; i++)
    cin >> a[i] >> b[i], a[i]--, b[i]--;

  n = n * 2;
  vector<int> c(n, -1);
  for (int i = 0; i < k; i++)
    c[a[i]] = b[i], c[b[i]] = a[i];

  auto circleMax = [&](int from, int x, int y) {
    int tmpx = x - from, tmpy = y - from;
    if (x < from)
      tmpx = x + n - from;
    if (y < from)
      tmpy = y + n - from;
    return tmpx < tmpy ? y : x;
  };
  vector<ll> res(n);
  // cerr<<circleMax(1,0,7)<<endl;

  // cerr<<"#DP Start"<<endl;
  int half = n / 2;

  for (int i = 0; i < n; i++) {
    bool b = (c[i] != -1 && c[i] < i);
    if (b)
      continue;
    for (int j = 0; j < n; j++)
      for (int k = 0; k <= half; k++)
        dpL[j][k] = dpM[j][k] = dpR[j][k] = 0;
    dpL[0][0] = 1;
    int cmax = i;
    for (int j = 0; j + 1 < n; j++) {
      int id = (i + j + 1) % n;

      if (c[id] == -1) {
        for (int k = 0; k + 1 <= half; k++) {
          dpL[j + 1][k + 1] += dpL[j][k];
          dpM[j + 1][k + 1] += dpM[j][k];
          dpR[j + 1][k + 1] += dpR[j][k];
        }
        for (int k = 1; k <= half; k++) {
          dpL[j + 1][k - 1] += dpL[j][k] * k;
          dpM[j + 1][k - 1] += dpM[j][k] * k;
          dpR[j + 1][k - 1] += dpR[j][k] * k;
        }
      } else {
        cmax = circleMax(i, cmax, c[id]);
        for (int k = 0; k <= half; k++) {
          dpL[j + 1][k] += dpL[j][k];
          dpM[j + 1][k] += dpM[j][k];
          dpR[j + 1][k] += dpR[j][k];
        }
      }
      if (c[id] == -1 && c[i] == -1 || c[id] == i) {
        for (int k = 0; k <= half; k++)
          dpM[j + 1][k] += dpL[j][k];
      }
      if (i < id && circleMax(i, id, cmax) == id) {
        dpR[j + 1][0] += dpM[j + 1][0];
        dpM[j + 1][0] = 0;
      }
      for (int k = 0; k <= half; k++)
        dpL[j + 1][k] %= MOD, dpM[j + 1][k] %= MOD, dpR[j + 1][k] %= MOD;
    }
    res[i] = dpR[n - 1][0];
  }
  // cerr<<"#DP END"<<endl;
  // for(int i=0;i<n;i++){
  //     cerr<<"$"<<i<<" "<<res[i]<<endl;
  // }

  cout << accumulate(res.begin(), res.end(), 0LL) % MOD << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
const ll MOD = 1e9 + 7;

ll dpL[601][601];
ll dpM[601][601];
ll dpR[601][601];

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(k), b(k);
  for (int i = 0; i < k; i++)
    cin >> a[i] >> b[i], a[i]--, b[i]--;

  n = n * 2;
  vector<int> c(n, -1);
  for (int i = 0; i < k; i++)
    c[a[i]] = b[i], c[b[i]] = a[i];

  auto circleMax = [&](int from, int x, int y) {
    int tmpx = x - from, tmpy = y - from;
    if (x < from)
      tmpx = x + n - from;
    if (y < from)
      tmpy = y + n - from;
    return tmpx < tmpy ? y : x;
  };
  vector<ll> res(n);
  // cerr<<circleMax(1,0,7)<<endl;

  // cerr<<"#DP Start"<<endl;
  int half = n / 2;

  for (int i = 0; i < n; i++) {
    bool b = (c[i] != -1 && c[i] < i);
    if (b)
      continue;
    for (int j = 0; j < n; j++)
      for (int k = 0; k <= half; k++)
        dpL[j][k] = dpM[j][k] = dpR[j][k] = 0;
    dpL[0][0] = 1;
    int cmax = i;
    for (int j = 0; j + 1 < n; j++) {
      int id = (i + j + 1) % n;

      if (c[id] == -1) {
        for (int k = 0; k + 1 <= half; k++) {
          dpL[j + 1][k + 1] += dpL[j][k];
          dpM[j + 1][k + 1] += dpM[j][k];
          dpR[j + 1][k + 1] += dpR[j][k];
        }
        for (int k = 1; k <= half; k++) {
          dpL[j + 1][k - 1] += dpL[j][k] * k;
          dpM[j + 1][k - 1] += dpM[j][k] * k;
          dpR[j + 1][k - 1] += dpR[j][k] * k;
        }
      } else {
        cmax = circleMax(i, cmax, c[id]);
        for (int k = 0; k <= half; k++) {
          dpL[j + 1][k] += dpL[j][k];
          dpM[j + 1][k] += dpM[j][k];
          dpR[j + 1][k] += dpR[j][k];
        }
      }
      if (c[id] == -1 && c[i] == -1 || c[id] == i) {
        for (int k = 0; k <= half; k++)
          dpM[j + 1][k] += dpL[j][k];
      }
      if (i < id && circleMax(i, id, cmax) == id) {
        dpR[j + 1][0] += dpM[j + 1][0];
        dpM[j + 1][0] = 0;
      }
      for (int k = 0; k <= half; k++)
        dpL[j + 1][k] %= MOD, dpM[j + 1][k] %= MOD, dpR[j + 1][k] %= MOD;
    }
    res[i] = dpR[n - 1][0];
  }
  // cerr<<"#DP END"<<endl;
  // for(int i=0;i<n;i++){
  //     cerr<<"$"<<i<<" "<<res[i]<<endl;
  // }

  cout << accumulate(res.begin(), res.end(), 0LL) % MOD << endl;
  return 0;
}