Problem p03003 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define double long double
#define endl "\n"
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define pb(x) push_back(x)
#define mp(a, b) make_pair(a, b)
#define ms(v, x) memset(v, x, sizeof(v))
#define pii pair<int, int>
#define ff first
#define ss second
#define frr(i, n) for (int i = 0; i < n; i++)
#define td(v) v.begin(), v.end()
#define inf 1000000000 // 1e9
#define M 1000000007   // 1e9 + 7
using namespace std;
inline int mod(int n, int m) {
  int ret = n % m;
  if (ret < 0)
    ret += m;
  return ret;
}
int gcd(int a, int b) { return (b == 0 ? a : gcd(b, a % b)); }
int lcm(int a, int b) { return (a * b) / gcd(a, b); }
int exp(int a, int b, int m) {
  if (b == 0)
    return 1;
  if (b == 1)
    return mod(a, m);
  int k = mod(exp(a, b / 2, m), m);
  if (b & 1) {
    return mod(a * mod(k * k, m), m);
  } else
    return mod(k * k, m);
}
int32_t main() {
  fastio;
  int n, m;
  cin >> n >> m;
  vector<int> a(n), b(m);
  frr(i, n) cin >> a[i];
  frr(i, m) cin >> b[i];
  int dp[n + 1][m + 1];
  frr(i, max(n, m) + 1) dp[i][0] = dp[0][i] = 1;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      dp[i][j] = mod(dp[i - 1][j] + dp[i][j - 1] -
                         (a[i - 1] == b[j - 1] ? 0 : dp[i - 1][j - 1]),
                     M);
  cout << dp[n][m] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define double long double
#define endl "\n"
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define pb(x) push_back(x)
#define mp(a, b) make_pair(a, b)
#define ms(v, x) memset(v, x, sizeof(v))
#define pii pair<int, int>
#define ff first
#define ss second
#define frr(i, n) for (int i = 0; i < n; i++)
#define td(v) v.begin(), v.end()
#define inf 1000000000 // 1e9
#define M 1000000007   // 1e9 + 7
using namespace std;
inline int mod(int n, int m) {
  int ret = n % m;
  if (ret < 0)
    ret += m;
  return ret;
}
int gcd(int a, int b) { return (b == 0 ? a : gcd(b, a % b)); }
int lcm(int a, int b) { return (a * b) / gcd(a, b); }
int exp(int a, int b, int m) {
  if (b == 0)
    return 1;
  if (b == 1)
    return mod(a, m);
  int k = mod(exp(a, b / 2, m), m);
  if (b & 1) {
    return mod(a * mod(k * k, m), m);
  } else
    return mod(k * k, m);
}
int32_t main() {
  fastio;
  int n, m;
  cin >> n >> m;
  vector<int> a(n), b(m);
  frr(i, n) cin >> a[i];
  frr(i, m) cin >> b[i];
  int lim = max(n, m) + 1;
  int dp[lim][lim];
  frr(i, lim) dp[i][0] = dp[0][i] = 1;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      dp[i][j] = mod(dp[i - 1][j] + dp[i][j - 1] -
                         (a[i - 1] == b[j - 1] ? 0 : dp[i - 1][j - 1]),
                     M);
  cout << dp[n][m] << endl;
}