Problem p02828 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <vector>

using namespace std;

typedef long long ll;

const int MAXN = 405;
const ll MOD = 998244353;

int n, id;
int h[MAXN];

ll a[MAXN];
ll b[MAXN];
ll C[MAXN][MAXN];
ll pw2[MAXN];
ll f[MAXN << 1][2][MAXN];

queue<int> Q;

int newnode() {
  int res = 0;
  if (!Q.empty()) {
    res = Q.front();
    Q.pop();
  } else
    res = ++id;
  memset(f[res], 0, sizeof(f[res]));
  return res;
}

int solve(int l, int r, int d) {
  int x = newnode();
  if (l > r) {
    f[x][0][0] = f[x][1][0] = 1;
    return x;
  }
  for (int i = l; i <= r; i++)
    if (h[i] < d) {
      int u = solve(l, i - 1, d), v = solve(i + 1, r, d);
      for (int j = 0; j <= i - l; j++)
        for (int k = 0; k <= r - i; k++) {
          (f[x][0][j + k] += f[u][0][j] * f[v][0][k]) %= MOD;
          (f[x][1][j + k + 1] += f[u][1][j] * f[v][1][k]) %= MOD;
        }
      Q.push(u);
      Q.push(v);
      return x;
    }
  int u = solve(l, r, d + 1);
  for (int j = 0; j <= r - l + 1; j++) {
    (f[x][0][j] += f[u][1][j]) %= MOD;
    (f[x][1][j] += f[u][1][j]) %= MOD;
  }
  for (int j = 0; j <= r - l + 1; j++) {
    (f[x][0][j] += f[u][0][j] * (pw2[r - l + 1 - j] - 1)) %= MOD;
    (f[x][1][j] += f[u][1][j] * (pw2[r - l + 1 - j] - 1)) %= MOD;
  }
  for (int j = 1; j <= r - l + 1; j++)
    for (int k = 1; k <= j; k++) {
      (f[x][0][j - k] += f[u][0][j] * C[j][k] % MOD * pw2[r - l + 1 - j]) %=
          MOD;
      (f[x][1][j - k] += f[u][1][j] * C[j][k] % MOD * pw2[r - l + 1 - j]) %=
          MOD;
    }
  Q.push(u);
  return x;
}

int main() {
  scanf("%d", &n);
  pw2[0] = 1;
  for (int i = 1; i <= n; i++)
    pw2[i] = pw2[i - 1] * 2 % MOD;
  for (int i = 0; i <= n; i++) {
    C[i][0] = 1;
    for (int j = 1; j <= i; j++)
      C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % MOD;
  }
  for (int i = 1; i <= n; i++)
    scanf("%d", &h[i]);
  solve(1, n, 1);
  printf("%lld\n", f[1][0][0]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <vector>

using namespace std;

typedef long long ll;

const int MAXN = 405;
const ll MOD = 998244353;

int n, id;
int h[MAXN];

ll a[MAXN];
ll b[MAXN];
ll C[MAXN][MAXN];
ll pw2[MAXN];
ll f[MAXN << 2][2][MAXN];

queue<int> Q;

int newnode() {
  int res = 0;
  if (!Q.empty()) {
    res = Q.front();
    Q.pop();
  } else
    res = ++id;
  memset(f[res], 0, sizeof(f[res]));
  return res;
}

int solve(int l, int r, int d) {
  int x = newnode();
  if (l > r) {
    f[x][0][0] = f[x][1][0] = 1;
    return x;
  }
  for (int i = l; i <= r; i++)
    if (h[i] < d) {
      int u = solve(l, i - 1, d), v = solve(i + 1, r, d);
      for (int j = 0; j <= i - l; j++)
        for (int k = 0; k <= r - i; k++) {
          (f[x][0][j + k] += f[u][0][j] * f[v][0][k]) %= MOD;
          (f[x][1][j + k + 1] += f[u][1][j] * f[v][1][k]) %= MOD;
        }
      Q.push(u);
      Q.push(v);
      return x;
    }
  int u = solve(l, r, d + 1);
  for (int j = 0; j <= r - l + 1; j++) {
    (f[x][0][j] += f[u][1][j]) %= MOD;
    (f[x][1][j] += f[u][1][j]) %= MOD;
  }
  for (int j = 0; j <= r - l + 1; j++) {
    (f[x][0][j] += f[u][0][j] * (pw2[r - l + 1 - j] - 1)) %= MOD;
    (f[x][1][j] += f[u][1][j] * (pw2[r - l + 1 - j] - 1)) %= MOD;
  }
  for (int j = 1; j <= r - l + 1; j++)
    for (int k = 1; k <= j; k++) {
      (f[x][0][j - k] += f[u][0][j] * C[j][k] % MOD * pw2[r - l + 1 - j]) %=
          MOD;
      (f[x][1][j - k] += f[u][1][j] * C[j][k] % MOD * pw2[r - l + 1 - j]) %=
          MOD;
    }
  Q.push(u);
  return x;
}

int main() {
  scanf("%d", &n);
  pw2[0] = 1;
  for (int i = 1; i <= n; i++)
    pw2[i] = pw2[i - 1] * 2 % MOD;
  for (int i = 0; i <= n; i++) {
    C[i][0] = 1;
    for (int j = 1; j <= i; j++)
      C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % MOD;
  }
  for (int i = 1; i <= n; i++)
    scanf("%d", &h[i]);
  solve(1, n, 1);
  printf("%lld\n", f[1][0][0]);
  return 0;
}