Problem p03349 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
typedef long double ld;
const ld INF = (ld)10000000000000;
typedef pair<int, int> P;
#define stop                                                                   \
  char nyaa;                                                                   \
  cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;

int n;
ll k, m;

ll pas[301][301];
void init() {
  rep(i, 303) {
    rep(j, 303) {
      if (i == 0 || j == 0)
        pas[i][j] = 1;
      else
        pas[i][j] = (pas[i - 1][j] + pas[i][j - 1]) % m;
    }
  }
}

ll dp[303][303];
ll rdp[303][303];
int main() {
  cin >> n >> k >> m;
  init();
  rep(i, k + 1) {
    dp[1][i] = 1;
    rdp[1][i + 1] = (rdp[1][i] + dp[1][i]) % m;
  }
  Rep1(i, 2, n + 1) {
    rep(j, k + 1) {
      rep1(l, i - 1) {
        (dp[i][j] += pas[i - 2 - l + 1][l - 1] * dp[i - l][j] % m *
                     ((rdp[l][k + 1] - rdp[l][j + 1]) % m + m) % m) %= m;
      }
      rdp[i][j + 1] = (rdp[i][j] + dp[i][j]) % m;
    }
  }
  // cout << dp[2][0] << endl;
  // cout << dp[2][1] << endl;
  // cout << dp[2][2] << endl;
  cout << dp[n + 1][0] << endl;
  // stop
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
typedef long double ld;
const ld INF = (ld)10000000000000;
typedef pair<int, int> P;
#define stop                                                                   \
  char nyaa;                                                                   \
  cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;

int n;
ll k, m;

ll pas[303][303];
void init() {
  rep(i, 303) {
    rep(j, 303) {
      if (i == 0 || j == 0)
        pas[i][j] = 1;
      else
        pas[i][j] = (pas[i - 1][j] + pas[i][j - 1]) % m;
    }
  }
}

ll dp[303][303];
ll rdp[303][303];
int main() {
  cin >> n >> k >> m;
  init();
  rep(i, k + 1) {
    dp[1][i] = 1;
    rdp[1][i + 1] = (rdp[1][i] + dp[1][i]) % m;
  }
  Rep1(i, 2, n + 1) {
    rep(j, k + 1) {
      rep1(l, i - 1) {
        (dp[i][j] += pas[i - 2 - l + 1][l - 1] * dp[i - l][j] % m *
                     ((rdp[l][k + 1] - rdp[l][j + 1]) % m + m) % m) %= m;
      }
      rdp[i][j + 1] = (rdp[i][j] + dp[i][j]) % m;
    }
  }
  // cout << dp[2][0] << endl;
  // cout << dp[2][1] << endl;
  // cout << dp[2][2] << endl;
  cout << dp[n + 1][0] << endl;
  // stop
  return 0;
}