Problem p02662 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#define mkp make_pair
#define mkt make_tuple
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD = 998244353;
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

#define MAX_N 1000010
ll inv[MAX_N + 10], fac[MAX_N + 10], ifac[MAX_N + 10];

void setComb() {
  inv[0] = 1;
  inv[1] = 1;
  fac[1] = 1;
  ifac[1] = 1;
  fac[0] = 1;
  ifac[0] = 1;
  for (int i = 2; i < MAX_N; i++) {
    inv[i] = (-MOD / i) * inv[MOD % i] % MOD;
    fac[i] = fac[i - 1] * i % MOD;
    ifac[i] = ifac[i - 1] * inv[i] % MOD;

    inv[i] = (inv[i] + MOD) % MOD;
    fac[i] = (fac[i] + MOD) % MOD;
    ifac[i] = (ifac[i] + MOD) % MOD;
  }
  return;
}

ll comb(ll n, ll k) {
  if (n < k || n < 0 || k < 0)
    return 0;
  else
    return ((fac[n] * ifac[k] % MOD * ifac[n - k] % MOD + MOD) % MOD);
}

ll hcomb(ll n, ll r) { // this size is really ok??
  if (n == 0 && r == 0)
    return 1;
  else if (n < 0 || r < 0)
    return 0;
  else
    return comb(n + r - 1, r);
}

ll mod_pow(ll x, ll n) {
  x %= MOD;
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % MOD;
    x = x * x % MOD;
    n >>= 1;
  }
  return res;
}

void add(ll &a, ll b) { a = (a + b) % MOD; }

void mul(ll &a, ll b) {
  a %= MOD;
  b %= MOD;
  a = a * b % MOD;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, S;
  cin >> N >> S;
  vector<int> A(N);
  rep(i, N) cin >> A[i];

  vector<vector<ll>> dp(N + 1, vector<ll>(S, 0));
  dp[0][0] = 1;
  for (int i = 0; i < N; i++)
    for (int j = 0; j <= S; j++) {
      if (dp[i][j] == 0)
        continue;
      add(dp[i + 1][j], dp[i][j] * 2 % MOD);
      if (j + A[i] <= S)
        add(dp[i + 1][j + A[i]], dp[i][j]);
    }

  cout << dp[N][S] << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#define mkp make_pair
#define mkt make_tuple
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD = 998244353;
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

#define MAX_N 1000010
ll inv[MAX_N + 10], fac[MAX_N + 10], ifac[MAX_N + 10];

void setComb() {
  inv[0] = 1;
  inv[1] = 1;
  fac[1] = 1;
  ifac[1] = 1;
  fac[0] = 1;
  ifac[0] = 1;
  for (int i = 2; i < MAX_N; i++) {
    inv[i] = (-MOD / i) * inv[MOD % i] % MOD;
    fac[i] = fac[i - 1] * i % MOD;
    ifac[i] = ifac[i - 1] * inv[i] % MOD;

    inv[i] = (inv[i] + MOD) % MOD;
    fac[i] = (fac[i] + MOD) % MOD;
    ifac[i] = (ifac[i] + MOD) % MOD;
  }
  return;
}

ll comb(ll n, ll k) {
  if (n < k || n < 0 || k < 0)
    return 0;
  else
    return ((fac[n] * ifac[k] % MOD * ifac[n - k] % MOD + MOD) % MOD);
}

ll hcomb(ll n, ll r) { // this size is really ok??
  if (n == 0 && r == 0)
    return 1;
  else if (n < 0 || r < 0)
    return 0;
  else
    return comb(n + r - 1, r);
}

ll mod_pow(ll x, ll n) {
  x %= MOD;
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % MOD;
    x = x * x % MOD;
    n >>= 1;
  }
  return res;
}

void add(ll &a, ll b) { a = (a + b) % MOD; }

void mul(ll &a, ll b) {
  a %= MOD;
  b %= MOD;
  a = a * b % MOD;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, S;
  cin >> N >> S;
  vector<int> A(N);
  rep(i, N) cin >> A[i];

  vector<vector<ll>> dp(N + 1, vector<ll>(S + 1, 0));
  dp[0][0] = 1;
  for (int i = 0; i < N; i++)
    for (int j = 0; j <= S; j++) {
      if (dp[i][j] == 0)
        continue;
      add(dp[i + 1][j], dp[i][j] * 2 % MOD);
      if (j + A[i] <= S)
        add(dp[i + 1][j + A[i]], dp[i][j]);
    }

  cout << dp[N][S] << endl;

  return 0;
}