Problem p03172 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define forn(i, n) for (ll i = 0; i < n; i++)
#define fore(i, a, b) for (ll i = a; i <= b; i++)
#define ford(i, n) for (ll i = n - 1; i >= 0; i--)
#define fi first
#define se second
#define endl "\n"
#define all(a) a.begin(), a.end()
#define sync                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define PI 3.14159265

/*************************************************************************************/
void file() {
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
}
/*************************************************************************************/

const ll maxn = 2e5 + 1, mod = 1e9 + 7;

int main() {
  sync
      //  file();
      int n,
      k;
  cin >> n >> k;
  vector<ll> dp(k + 1);
  dp[0] = 1;
  forn(i, n) {
    int x;
    cin >> x;
    vector<int> fake(k + 1);
    for (int j = k; j >= 0; --j) {
      int l = j + 1;
      int r = j + min(x, k - j) + 1;
      if (l < r && l <= k) {
        fake[l] += dp[j];
        fake[l] %= mod;
        fake[r] -= dp[j];
        fake[r] += mod;
        fake[r] %= mod;
      }
    }
    ll pre = 0;
    fore(lel, 0, k) {
      pre += fake[lel];
      pre %= mod;
      dp[lel] += pre;
      dp[lel] %= mod;
    }
  }
  cout << dp[k] << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define forn(i, n) for (ll i = 0; i < n; i++)
#define fore(i, a, b) for (ll i = a; i <= b; i++)
#define ford(i, n) for (ll i = n - 1; i >= 0; i--)
#define fi first
#define se second
#define endl "\n"
#define all(a) a.begin(), a.end()
#define sync                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define PI 3.14159265

/*************************************************************************************/
void file() {
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
}
/*************************************************************************************/

const ll maxn = 2e5 + 1, mod = 1e9 + 7;

int main() {
  sync
      //  file();
      int n,
      k;
  cin >> n >> k;
  vector<ll> dp(k + 1);
  dp[0] = 1;
  forn(i, n) {
    int x;
    cin >> x;
    vector<int> fake(k + 1);
    for (int j = k; j >= 0; --j) {
      int l = j + 1;
      int r = j + min(x, k - j) + 1;
      if (l < r && l <= k) {
        fake[l] += dp[j];
        fake[l] %= mod;
        if (r <= k) {
          fake[r] -= dp[j];
          fake[r] += mod;
          fake[r] %= mod;
        }
      }
    }
    ll pre = 0;
    fore(lel, 0, k) {
      pre += fake[lel];
      pre %= mod;
      dp[lel] += pre;
      dp[lel] %= mod;
    }
  }
  cout << dp[k] << endl;

  return 0;
}