Problem p03989 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long LL;
const LL mo = 924844033;
const int N = 1000;
int n, k;
LL f[N + 10], g[N + 10][N + 10][2], h[N + 10], fac[N + 10];
int main() {
  scanf("%d%d", &n, &k);
  f[0] = 1;
  for (int i = 1; i <= k * 2; i++) {
    g[0][0][0] = 1;
    g[0][0][1] = 0;
    int maxp = 0;
    for (int j = i, p = 1; j <= n; j += k * 2, p++) {
      maxp = max(maxp, p);
      for (int kx = 0; kx <= p; kx++)
        g[p][kx][0] = g[p][kx][1] = 0;
    }
    for (int j = i, p = 1; j <= n; j += k * 2, p++)
      for (int kx = 0; kx < p; kx++) {
        LL nowx = g[p - 1][kx][0];
        if (j + k <= n)
          g[p][kx + 1][1] = (g[p][kx + 1][1] + nowx) % mo;
        if (j - k > 0)
          g[p][kx + 1][0] = (g[p][kx + 1][0] + nowx) % mo;
        g[p][kx][0] = (g[p][kx][0] + nowx) % mo;
        nowx = g[p - 1][kx][1];
        if (j + k <= n)
          g[p][kx + 1][1] = (g[p][kx + 1][1] + nowx) % mo;
        g[p][kx][0] = (g[p][kx][0] + nowx) % mo;
      }
    for (int j = 0; j <= n; j++)
      h[j] = 0;
    for (int j = 0; j <= maxp; j++) {
      LL nowx = (g[maxp][j][0] + g[maxp][j][1]) % mo;
      for (int kx = 0; kx + j <= n; kx++)
        h[kx + j] = (h[kx + j] + f[kx] * nowx) % mo;
    }
    for (int j = 0; j <= n; j++)
      f[j] = h[j];
  }
  fac[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = fac[i - 1] * i % mo;
  LL ans = 0;
  for (int j = 0; j <= n; j++) {
    if (j % 2 == 0)
      ans = (ans + fac[n - j] * f[j]);
    else
      ans = (ans - fac[n - j] * f[j]) % mo;
  }
  ans = (ans % mo + mo) % mo;
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long LL;
const LL mo = 924844033;
const int N = 2000;
int n, k;
LL f[N + 10], g[N + 10][N + 10][2], h[N + 10], fac[N + 10];
int main() {
  scanf("%d%d", &n, &k);
  f[0] = 1;
  for (int i = 1; i <= k * 2; i++) {
    g[0][0][0] = 1;
    g[0][0][1] = 0;
    int maxp = 0;
    for (int j = i, p = 1; j <= n; j += k * 2, p++) {
      maxp = max(maxp, p);
      for (int kx = 0; kx <= p; kx++)
        g[p][kx][0] = g[p][kx][1] = 0;
    }
    for (int j = i, p = 1; j <= n; j += k * 2, p++)
      for (int kx = 0; kx < p; kx++) {
        LL nowx = g[p - 1][kx][0];
        if (j + k <= n)
          g[p][kx + 1][1] = (g[p][kx + 1][1] + nowx) % mo;
        if (j - k > 0)
          g[p][kx + 1][0] = (g[p][kx + 1][0] + nowx) % mo;
        g[p][kx][0] = (g[p][kx][0] + nowx) % mo;
        nowx = g[p - 1][kx][1];
        if (j + k <= n)
          g[p][kx + 1][1] = (g[p][kx + 1][1] + nowx) % mo;
        g[p][kx][0] = (g[p][kx][0] + nowx) % mo;
      }
    for (int j = 0; j <= n; j++)
      h[j] = 0;
    for (int j = 0; j <= maxp; j++) {
      LL nowx = (g[maxp][j][0] + g[maxp][j][1]) % mo;
      for (int kx = 0; kx + j <= n; kx++)
        h[kx + j] = (h[kx + j] + f[kx] * nowx) % mo;
    }
    for (int j = 0; j <= n; j++)
      f[j] = h[j];
  }
  fac[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = fac[i - 1] * i % mo;
  LL ans = 0;
  for (int j = 0; j <= n; j++) {
    if (j % 2 == 0)
      ans = (ans + fac[n - j] * f[j]);
    else
      ans = (ans - fac[n - j] * f[j]) % mo;
  }
  ans = (ans % mo + mo) % mo;
  printf("%lld\n", ans);
  return 0;
}