Problem p01769 (C++)

Buggy submission (status: Runtime Error):
// include
//--------------------------------------------------
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define int long long

// conversion
//--------------------------------------------------
inline int toInt(const string &s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// math
//--------------------------------------------------
template <class T> inline T sqr(T x) { return x * x; }

// typedef
//--------------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//--------------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//--------------------------------------------------
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RREP(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define RFOR(i, a, b) for (int i = (a)-1; i >= (b); i--)

// clear memory
//--------------------------------------------------
#define CLR(a) memset((a), 0, sizeof(a))
#define CLRM(a) memset((a), -1, sizeof(a))
// end-----------------------------------------------

int bit_n, dat[10000];
void init(int n) {
  bit_n = n;
  CLR(dat);
}
void add(int k, int x) {
  for (k++; k <= bit_n; k += k & -k)
    dat[k] += x;
}
int sum(int k) {
  int ret = 0;
  for (k++; k; k -= k & -k)
    ret += dat[k];
  return ret;
}
int del(int k) {
  int v = sum(k) - sum(k - 1);
  add(k, -v);
}

const int mod = 1e9 + 7;
int N, L;
int x[5000], a[5000];
signed main() {
  cin >> N >> L;
  REP(i, N) cin >> x[i];
  REP(i, N) cin >> a[i];

  init(L + 114);
  add(L, 1);
  RREP(i, N) {
    REP(j, L + 1) {
      del(j);
      if (j < x[i] || (j - x[i]) % a[i])
        continue;
      int v = sum(L) - sum(j);
      add(j, v % mod);
    }
  }

  cout << sum(L) % mod << endl;
  return 0;
}

Fixed submission:
// include
//--------------------------------------------------
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define int long long

// conversion
//--------------------------------------------------
inline int toInt(const string &s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// math
//--------------------------------------------------
template <class T> inline T sqr(T x) { return x * x; }

// typedef
//--------------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//--------------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//--------------------------------------------------
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RREP(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define RFOR(i, a, b) for (int i = (a)-1; i >= (b); i--)

// clear memory
//--------------------------------------------------
#define CLR(a) memset((a), 0, sizeof(a))
#define CLRM(a) memset((a), -1, sizeof(a))
// end-----------------------------------------------

int bit_n, dat[10000];
void init(int n) {
  bit_n = n;
  CLR(dat);
}
void add(int k, int x) {
  for (k++; k <= bit_n; k += k & -k)
    dat[k] += x;
}
int sum(int k) {
  int ret = 0;
  for (k++; k; k -= k & -k)
    ret += dat[k];
  return ret;
}
int del(int k) {
  int v = sum(k) - sum(k - 1);
  add(k, -v);
}

const int mod = 1e9 + 7;
int N, L;
int x[5000], a[5000];
signed main() {
  cin >> N >> L;
  REP(i, N) cin >> x[i];
  REP(i, N) cin >> a[i];

  init(L + 114);
  add(L, 1);
  RREP(i, N) {
    REP(j, L + 1) {
      del(j);
      if (j < x[i] || (a[i] == 0 && j != x[i]) || (j - x[i]) % a[i])
        continue;
      int v = sum(L) - sum(j);
      add(j, v % mod);
    }
  }

  cout << sum(L) % mod << endl;
  return 0;
}