Problem p02662 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
// #include"testlib.h"
#define endl "\n"
#define all(v) v.begin(), v.end()
#define allr(s) s.rbegin(), s.rend()
#define RT(s) return cout << s, 0
#define watch(x) cout << (#x) << " = " << x << endl
#define sz(s) (int)(s.size())
#define PI acos(-1)
#define EPS 1e-8
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};
int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};
void file() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
#else
  // freopen("street.in", "r", stdin);
  // freopen("out.txt", "w", stdout);
#endif
}
void fast() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
}
const int N = 1e3 + 9;
ll mem[N][N];
int mod = 998244353;
int n, s;
vi v1;
ll fpow(ll x, ll p) {
  if (!p)
    return 1;
  ll sq = fpow(x, p / 2);
  sq *= sq;
  sq %= mod;
  if (p & 1)
    sq *= x;
  return sq % mod;
}
ll solve(int idx, int rem) {
  if (rem < 0)
    return 0;
  if (!rem)
    return fpow(2, n - (ll)idx);
  if (idx == n)
    return 0;
  ll &ret = mem[idx][rem];
  if (ret != -1)
    return ret;
  ret = 2 * solve(idx + 1, rem) + solve(idx + 1, rem - v1[idx]);
  ret %= mod;
  return ret;
}
int main() {
  //	file();
  fast();
  memset(mem, -1, sizeof mem);
  cin >> n >> s;
  v1 = vi(n);
  for (auto &it : v1)
    cin >> it;
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    // watch(solve(i + 1, s - v1[i]));
    ans += fpow(2, i) * solve(i + 1, s - v1[i]);
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
// #include"testlib.h"
#define endl "\n"
#define all(v) v.begin(), v.end()
#define allr(s) s.rbegin(), s.rend()
#define RT(s) return cout << s, 0
#define watch(x) cout << (#x) << " = " << x << endl
#define sz(s) (int)(s.size())
#define PI acos(-1)
#define EPS 1e-8
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};
int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};
void file() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
#else
  // freopen("street.in", "r", stdin);
  // freopen("out.txt", "w", stdout);
#endif
}
void fast() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
}
const int N = 3e3 + 9;
ll mem[N][N];
int mod = 998244353;
int n, s;
vi v1;
ll fpow(ll x, ll p) {
  if (!p)
    return 1;
  ll sq = fpow(x, p / 2);
  sq *= sq;
  sq %= mod;
  if (p & 1)
    sq *= x;
  return sq % mod;
}
ll solve(int idx, int rem) {
  if (rem < 0)
    return 0;
  if (!rem)
    return fpow(2, n - (ll)idx);
  if (idx == n)
    return 0;
  ll &ret = mem[idx][rem];
  if (ret != -1)
    return ret;
  ret = 2 * solve(idx + 1, rem) + solve(idx + 1, rem - v1[idx]);
  ret %= mod;
  return ret;
}
int main() {
  //	file();
  fast();
  memset(mem, -1, sizeof mem);
  cin >> n >> s;
  v1 = vi(n);
  for (auto &it : v1)
    cin >> it;
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    // watch(solve(i + 1, s - v1[i]));
    ans += fpow(2, i) * solve(i + 1, s - v1[i]);
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}