Problem p02662 (C++) — buggy submission, verdict: Runtime Error

/*
    Author - Param Kothari
    Ah shit, here we go again
*/

#include <bits/stdc++.h>

using namespace std;

#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define mk make_pair

typedef long long ll;
typedef long double ld;

const ll MOD = 998244353;

ll modp(ll x, ll y, ll p) {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) {
      res = (res * x) % p;
    }
    y = y >> 1;
    x = (x * x) % p;
  }
  return res % p;
}

void solve() {
  ll n, s;
  cin >> n >> s;
  vector<ll> a(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  ll dp[n + 1][s + 1];
  ll inv = modp(2, MOD - 2, MOD);
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= s; j++) {
      dp[i][j] = 0;
    }
  }
  dp[0][0] = modp(2, n, MOD);
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= s; j++) {
      dp[i][j] = dp[i - 1][j];
      if (j >= a[i]) {
        dp[i][j] += (dp[i - 1][j - a[i]] * inv);
      }
      dp[i][j] %= MOD;
    }
  }
  cout << dp[n][s] % MOD << "\\n";
  return;
}

int main() {
  IOS;
  int t = 1;
  // cin >> t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}

Error: -11

Fixed submission:

/*
    Author - Param Kothari
    Ah shit, here we go again
*/

#include <bits/stdc++.h>

using namespace std;

#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define mk make_pair

typedef long long ll;
typedef long double ld;

const ll MOD = 998244353, maxN = 3003;

ll n, s;
ll a[maxN];
ll dp[maxN][maxN];
vector<ll> all;

ll modp(ll x, ll y, ll p) {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) {
      res = (res * x) % p;
    }
    y = y >> 1;
    x = (x * x) % p;
  }
  return res % p;
}

void rec(int idx, int x, ll &res) {
  if (idx == 0 && x != 0 && dp[0][x]) {
    res++;
    all.pb(res);
    return;
  }
  if (idx == 0 && x == 0) {
    all.pb(res);
  }
  if (dp[idx - 1][x]) {
    ll res2 = res;
    rec(idx - 1, x, res2);
  }
  if (x >= a[idx] && dp[idx - 1][x - a[idx]]) {
    res++;
    rec(idx - 1, x - a[idx], res);
  }
  return;
}

void fill(ll x) {
  if (n == 0 || x < 0)
    return;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= x; j++) {
      dp[i][j] = 0;
    }
    dp[i][0] = 1;
  }
  if (x >= a[0]) {
    dp[0][a[0]] = 1;
  }
  for (int i = 1; i < n; i++) {
    for (int j = 0; j <= x; j++) {
      dp[i][j] =
          (a[i] <= j ? dp[i - 1][j] || dp[i - 1][j - a[i]] : dp[i - 1][j]);
    }
  }
  ll res = 0;
  rec(n - 1, x, res);
}

void solve() {
  cin >> n >> s;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  fill(s);
  ll res = 0;
  for (auto x : all) {
    if (x == 0)
      continue;
    res = res + modp(2, (n - x), MOD);
    res = res % MOD;
  }
  cout << res << "\\n";
  return;
}

int main() {
  IOS;
  int t = 1;
  // cin >> t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}