Problem p03003 (C++)

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

using namespace std;

#define _for(i, j, N) for (int i = (j); i < (N); i++)
#define _rep(i, j, N) for (int i = (j); i <= (N); i++)
#define ALL(x) x.begin(), x.end()
#define pb push_back
#define mk make_pair

typedef long long LL;
typedef pair<int, int> Interval;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  _for(i, 0, v.size()) os << v[i] << " ";
  return os;
}

template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
  for (typename set<T>::iterator it = v.begin(); it != v.end(); it++)
    os << *it << " ";
  return os;
}

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &v) {
  os << v.first << " " << v.second << endl;
  return os;
}

const int mod = 1e9 + 7;
const int maxn = 1e5 + 5;
const int maxh = 1e3 + 5;

int N, M;
vector<int> A;
vector<int> B;

int dp[maxh][maxh];

int add(LL a, LL b) { return (int)((a + b) % mod); }
int sub(LL a, LL b) { return (int)((a - b + mod) % mod); }

int main() {
  cin >> N >> M;
  _for(i, 0, N) {
    int num;
    scanf("%d", &num);
    A.push_back(num);
  }
  _for(i, 0, M) {
    int num;
    scanf("%d", &num);
    B.push_back(num);
  }
  _rep(i, 0, N) { dp[0][i] = 1; }
  _rep(i, 0, M) { dp[i][0] = 1; }

  _rep(i, 1, M) {
    _rep(j, 1, N) {
      if (B[i - 1] == A[j - 1]) {
        dp[i][j] = add(dp[i - 1][j], dp[i][j - 1]);
      } else {
        dp[i][j] = add(dp[i - 1][j], dp[i][j - 1]);
        dp[i][j] = sub(dp[i][j], dp[i - 1][j - 1]);
      }
    }
  }
  /*
  _rep(i,0,M){
      _rep(j,0,N){
          printf("%d ",dp[i][j]);
      }
      printf("\n");
  }
  */
  cout << dp[M][N] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define _for(i, j, N) for (int i = (j); i < (N); i++)
#define _rep(i, j, N) for (int i = (j); i <= (N); i++)
#define ALL(x) x.begin(), x.end()
#define pb push_back
#define mk make_pair

typedef long long LL;
typedef pair<int, int> Interval;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  _for(i, 0, v.size()) os << v[i] << " ";
  return os;
}

template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
  for (typename set<T>::iterator it = v.begin(); it != v.end(); it++)
    os << *it << " ";
  return os;
}

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &v) {
  os << v.first << " " << v.second << endl;
  return os;
}

const int mod = 1e9 + 7;
const int maxn = 1e5 + 5;
const int maxh = 2e3 + 5;

int N, M;
vector<int> A;
vector<int> B;

int dp[maxh][maxh];

int add(LL a, LL b) { return (int)((a + b) % mod); }
int sub(LL a, LL b) { return (int)((a - b + mod) % mod); }

int main() {
  cin >> N >> M;
  _for(i, 0, N) {
    int num;
    scanf("%d", &num);
    A.push_back(num);
  }
  _for(i, 0, M) {
    int num;
    scanf("%d", &num);
    B.push_back(num);
  }
  _rep(i, 0, N) { dp[0][i] = 1; }
  _rep(i, 0, M) { dp[i][0] = 1; }

  _rep(i, 1, M) {
    _rep(j, 1, N) {
      if (B[i - 1] == A[j - 1]) {
        dp[i][j] = add(dp[i - 1][j], dp[i][j - 1]);
      } else {
        dp[i][j] = add(dp[i - 1][j], dp[i][j - 1]);
        dp[i][j] = sub(dp[i][j], dp[i - 1][j - 1]);
      }
    }
  }
  /*
  _rep(i,0,M){
      _rep(j,0,N){
          printf("%d ",dp[i][j]);
      }
      printf("\n");
  }
  */
  cout << dp[M][N] << endl;
  return 0;
}