Problem p03003 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
// #include <boost/multiprecision/cpp_int.hpp>

const double EPS = (1e-10);

using namespace std;
using Int = long long;
// using namespace boost::multiprecision;

const Int MOD = 1000000007;

long long mod_pow(long long x, long long n) {
  long long res = 1;
  for (int i = 0; i < 60; i++) {
    if (n >> i & 1)
      res = res * x % MOD;
    x = x * x % MOD;
  }
  return res;
}

template <typename T> T gcd(T a, T b) { return b != 0 ? gcd(b, a % b) : a; }

template <typename T> T lcm(T a, T b) { return a * b / gcd(a, b); }

void fast_input() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

int dp[1005][1005];

int main(void) {
  int N, M;
  cin >> N >> M;
  vector<int> S(N + 1);
  vector<int> T(M + 1);
  S[0] = 0;
  T[0] = 0;
  for (int i = 1; i <= N; i++) {
    cin >> S[i];
  }
  for (int i = 1; i <= M; i++) {
    cin >> T[i];
  }
  for (int i = 0; i <= N; i++) {
    dp[i][0] = 1;
  }
  for (int i = 0; i <= M; i++) {
    dp[0][i] = 1;
  }
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= M; j++) {
      dp[i][j] += (dp[i - 1][j] - dp[i - 1][j - 1] + MOD) % MOD;
      dp[i][j] %= MOD;
      dp[i][j] += (dp[i][j - 1] - dp[i - 1][j - 1] + MOD) % MOD;
      dp[i][j] %= MOD;
      dp[i][j] += dp[i - 1][j - 1];
      dp[i][j] %= MOD;
      if (S[i] == T[j]) {
        dp[i][j] += dp[i - 1][j - 1];
        dp[i][j] %= MOD;
      }
    }
  }
  cout << dp[N][M] << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
// #include <boost/multiprecision/cpp_int.hpp>

const double EPS = (1e-10);

using namespace std;
using Int = long long;
// using namespace boost::multiprecision;

const Int MOD = 1000000007;

long long mod_pow(long long x, long long n) {
  long long res = 1;
  for (int i = 0; i < 60; i++) {
    if (n >> i & 1)
      res = res * x % MOD;
    x = x * x % MOD;
  }
  return res;
}

template <typename T> T gcd(T a, T b) { return b != 0 ? gcd(b, a % b) : a; }

template <typename T> T lcm(T a, T b) { return a * b / gcd(a, b); }

void fast_input() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

int dp[2005][2005];

int main(void) {
  int N, M;
  cin >> N >> M;
  vector<int> S(N + 1);
  vector<int> T(M + 1);
  S[0] = 0;
  T[0] = 0;
  for (int i = 1; i <= N; i++) {
    cin >> S[i];
  }
  for (int i = 1; i <= M; i++) {
    cin >> T[i];
  }
  for (int i = 0; i <= N; i++) {
    dp[i][0] = 1;
  }
  for (int i = 0; i <= M; i++) {
    dp[0][i] = 1;
  }
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= M; j++) {
      dp[i][j] += (dp[i - 1][j] - dp[i - 1][j - 1] + MOD) % MOD;
      dp[i][j] %= MOD;
      dp[i][j] += (dp[i][j - 1] - dp[i - 1][j - 1] + MOD) % MOD;
      dp[i][j] %= MOD;
      dp[i][j] += dp[i - 1][j - 1];
      dp[i][j] %= MOD;
      if (S[i] == T[j]) {
        dp[i][j] += dp[i - 1][j - 1];
        dp[i][j] %= MOD;
      }
    }
  }
  cout << dp[N][M] << endl;
}