Problem p03003 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
using P = pair<long, long>;

typedef long long int ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

#define fillInt(xs)                                                            \
  for (int i = 0; i < (xs).size(); i++)                                        \
    scanf("%d", &xs[i]);
#define fillLong(xs)                                                           \
  for (int i = 0; i < (xs).size(); i++)                                        \
    scanf("%ld", &xs[i]);
#define fillDouble(xs)                                                         \
  for (int i = 0; i < (xs).size(); i++)                                        \
    scanf("%lf", &xs[i]);
#define fillString(xs)                                                         \
  for (int i = 0; i < (xs).size(); i++)                                        \
    cin >> xs[i];
#define sortv(xs) sort(xs.begin(), xs.end())
#define sortvinv(xs) sort(xs.begin(), xs.end(), std::greater<long>())
#define lbv(xs, x) lower_bound(xs.begin(), xs.end(), x) - xs.begin()
#define ubv(xs, x) upper_bound(xs.begin(), xs.end(), x) - xs.begin()
#define bs(xs, x) binary_search(xs.begin(), xs.end(), x)
#define index_of(as, x)                                                        \
  distance(as.begin(), lower_bound(as.begin(), as.end(), x))
#define rad_to_deg(rad) (((rad) / 2 / M_PI) * 360)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define concat(xs, ys) (xs).insert((xs).end(), (ys).begin(), (ys).end())

const int mod = 1000000007;
const int MAX_V = 100005;

struct mint {
  ll x;
  mint(ll x = 0) : x(x % mod) {}

  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }

  mint &operator-=(const mint a) {
    if (x < a.x) {
      x += mod;
    }
    x -= a.x;
    return *this;
  }

  mint &operator*=(const mint a) {
    x = (x * a.x) % mod;
    return *this;
  }

  mint &operator/=(const mint a) {
    *this *= a.inv();
    return *this;
  }

  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }

  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }

  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }

  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }

  mint pow(ll n) const {
    mint a(*this);
    mint res = 1;
    while (n > 0) {
      if (n & 1)
        res *= a;
      a *= a;
      n >>= 1;
    }
    return res;
  }

  mint inv() const { return pow(mod - 2); }
};

mint dp[1005][1005];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;

  vector<long> s(n);
  vector<long> t(m);

  fillLong(s);
  fillLong(t);

  rep(i, n + 1) { dp[i][0] = 1; }

  rep(i, m + 1) { dp[0][i] = 1; }

  rep(i, n) rep(j, m) {
    dp[i + 1][j + 1] = dp[i][j + 1] + dp[i + 1][j];
    if (s[i] != t[j]) {
      dp[i + 1][j + 1] -= dp[i][j];
    }
  }

  cout << dp[n][m].x << endl;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
using P = pair<long, long>;

typedef long long int ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

#define fillInt(xs)                                                            \
  for (int i = 0; i < (xs).size(); i++)                                        \
    scanf("%d", &xs[i]);
#define fillLong(xs)                                                           \
  for (int i = 0; i < (xs).size(); i++)                                        \
    scanf("%ld", &xs[i]);
#define fillDouble(xs)                                                         \
  for (int i = 0; i < (xs).size(); i++)                                        \
    scanf("%lf", &xs[i]);
#define fillString(xs)                                                         \
  for (int i = 0; i < (xs).size(); i++)                                        \
    cin >> xs[i];
#define sortv(xs) sort(xs.begin(), xs.end())
#define sortvinv(xs) sort(xs.begin(), xs.end(), std::greater<long>())
#define lbv(xs, x) lower_bound(xs.begin(), xs.end(), x) - xs.begin()
#define ubv(xs, x) upper_bound(xs.begin(), xs.end(), x) - xs.begin()
#define bs(xs, x) binary_search(xs.begin(), xs.end(), x)
#define index_of(as, x)                                                        \
  distance(as.begin(), lower_bound(as.begin(), as.end(), x))
#define rad_to_deg(rad) (((rad) / 2 / M_PI) * 360)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define concat(xs, ys) (xs).insert((xs).end(), (ys).begin(), (ys).end())

const int mod = 1000000007;
const int MAX_V = 100005;

struct mint {
  ll x;
  mint(ll x = 0) : x(x % mod) {}

  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }

  mint &operator-=(const mint a) {
    if (x < a.x) {
      x += mod;
    }
    x -= a.x;
    return *this;
  }

  mint &operator*=(const mint a) {
    x = (x * a.x) % mod;
    return *this;
  }

  mint &operator/=(const mint a) {
    *this *= a.inv();
    return *this;
  }

  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }

  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }

  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }

  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }

  mint pow(ll n) const {
    mint a(*this);
    mint res = 1;
    while (n > 0) {
      if (n & 1)
        res *= a;
      a *= a;
      n >>= 1;
    }
    return res;
  }

  mint inv() const { return pow(mod - 2); }
};

mint dp[2005][2005];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;

  vector<long> s(n);
  vector<long> t(m);

  fillLong(s);
  fillLong(t);

  rep(i, n + 1) { dp[i][0] = 1; }

  rep(i, m + 1) { dp[0][i] = 1; }

  rep(i, n) rep(j, m) {
    dp[i + 1][j + 1] = dp[i][j + 1] + dp[i + 1][j];
    if (s[i] != t[j]) {
      dp[i + 1][j + 1] -= dp[i][j];
    }
  }

  cout << dp[n][m].x << endl;
}