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

#include "bits/stdc++.h"
using namespace std;
#define REP(i, n) for (ll i = 0; i < n; i++)
#define ll long long
#define MOD 998244353LL
#define llMAX 9223372036854775807LL
#define llMIN -9223372036854775808LL
using vi = vector<ll>;  // intの1次元の型に vi という別名をつける
using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける

ll n, s;

ll moddiv(ll a, ll b) {
  ll x0 = MOD, x1 = b, x2, n0 = 0LL, n1 = 1LL, n2, t = a % b, m, ans;
  if (t == 0)
    return a / b;
  for (int i = 0; i < 900; i++) {
    m = x0 / x1;
    x2 = x0 - x1 * m;
    n2 = (n0 - m * n1) % MOD;
    if (x2 == 1) {
      ans = (n2 + MOD) % MOD;
      break;
    }
    x0 = x1;
    x1 = x2;
    n0 = n1;
    n1 = n2;
  }
  return (a + ((t * ans) % MOD) * b - t) / b;
}
// 剰余の割ざん

// aのn乗 mod c
ll expmod(ll a, ll n) {
  ll ans = 1LL, aa = a, beki = n;
  for (int i = 0; i < 64; i++) {
    if (beki % 2 == 1)
      ans = ans * aa % MOD;
    aa = aa * aa % MOD;
    beki /= 2;
    if (beki == 0)
      break;
  }
  return ans;
}

// nCk
vi mulmod(500001); // 0!,1!,2!,3!,4!,,,,
void first_nCk() {
  mulmod[0] = 1;
  for (ll i = 1; i < 500001; i++) {
    mulmod[i] = mulmod[i - 1] * i % MOD;
  }
}
ll nCk(ll nn, ll kk) {
  return moddiv(mulmod[nn], mulmod[kk] * mulmod[nn - kk] % MOD);
}

int main() {
  ll ans = 0;
  cin >> n >> s;
  vi a(n), b(n);
  REP(i, n) cin >> a[i];

  vi dp(s * 2 + 2), dp2(s * 2 + 2);

  dp[0] = expmod(2LL, n);
  // cout<<dp[0]<<endl;

  for (ll i = 0; i < n; i++) {

    for (ll j = 0; j < a[i]; j++) {
      dp2[j] = dp[j];
      if (j == s)
        break;
    }

    for (ll j = 0; j < s + 1; j++) {
      if (j + a[i] <= s) {
        dp2[j + a[i]] = (moddiv(dp[j], 2LL) + dp[j + a[i]]) % MOD;
      }
    }

    for (ll j = 0; j < s + 1; j++) {
      dp[j] = dp2[j];
    }
    /*
        cout<<endl;
        for(ll j=0;j<s+1;j++){
          cout<<dp[j]<<endl;
        }
        cout<<endl;
      */
  }

  ans = dp[s];
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#define REP(i, n) for (ll i = 0; i < n; i++)
#define ll long long
#define MOD 998244353LL
#define llMAX 9223372036854775807LL
#define llMIN -9223372036854775808LL
using vi = vector<ll>;  // intの1次元の型に vi という別名をつける
using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける

ll n, s;

ll moddiv(ll a, ll b) {
  ll x0 = MOD, x1 = b, x2, n0 = 0LL, n1 = 1LL, n2, t = a % b, m, ans;
  if (t == 0)
    return a / b;
  for (int i = 0; i < 900; i++) {
    m = x0 / x1;
    x2 = x0 - x1 * m;
    n2 = (n0 - m * n1) % MOD;
    if (x2 == 1) {
      ans = (n2 + MOD) % MOD;
      break;
    }
    x0 = x1;
    x1 = x2;
    n0 = n1;
    n1 = n2;
  }
  return (a + ((t * ans) % MOD) * b - t) / b;
}
// 剰余の割ざん

// aのn乗 mod c
ll expmod(ll a, ll n) {
  ll ans = 1LL, aa = a, beki = n;
  for (int i = 0; i < 64; i++) {
    if (beki % 2 == 1)
      ans = ans * aa % MOD;
    aa = aa * aa % MOD;
    beki /= 2;
    if (beki == 0)
      break;
  }
  return ans;
}

// nCk
vi mulmod(500001); // 0!,1!,2!,3!,4!,,,,
void first_nCk() {
  mulmod[0] = 1;
  for (ll i = 1; i < 500001; i++) {
    mulmod[i] = mulmod[i - 1] * i % MOD;
  }
}
ll nCk(ll nn, ll kk) {
  return moddiv(mulmod[nn], mulmod[kk] * mulmod[nn - kk] % MOD);
}

int main() {
  ll ans = 0;
  cin >> n >> s;
  vi a(n), b(n);
  REP(i, n) cin >> a[i];

  vi dp(s + 1), dp2(s + 1);

  dp[0] = expmod(2LL, n);
  // cout<<dp[0]<<endl;

  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < s + 1; j++) {
      for (ll j = 0; j < a[i]; j++) {
        dp2[j] = dp[j];
      }
      if (j + a[i] <= s) {
        dp2[j + a[i]] = moddiv(dp[j], 2LL) + dp[j + a[i]];
      }
    }

    for (ll j = 0; j < s + 1; j++) {
      dp[j] = dp2[j];
    }
    /*
        cout<<endl;
        for(ll j=0;j<s+1;j++){
          cout<<dp[j]<<endl;
        }
        cout<<endl;
      */
  }

  ans = dp[s];
  cout << ans << endl;
  return 0;
}