Problem p03003 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define lowbit(x) (x & (-x))
#define ll long long
#define ull unsigned long long
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a > b ? b : a)
#define PI acos(-1.0)
#define fir first
#define sec second
#define SZ(x) (x.size())
#define inf 0x3f3f3f3f
#define PB push_back
#define MP make_pair
#define all(x) (x).begin(), (x).end()
#define PII pair<int, int>
#define E 2.7182818284590452353602874713527
using namespace std;

/**********************************************Head-----Template****************************************/
bool Finish_read;
template <class T> inline void read(T &x) {
  Finish_read = 0;
  x = 0;
  int f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    if (ch == EOF)
      return;
    ch = getchar();
  }
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  x *= f;
  Finish_read = 1;
}
template <class T> inline void print(T x) {
  if (x / 10 != 0)
    print(x / 10);
  putchar(x % 10 + '0');
}
template <class T> inline void writeln(T x) {
  if (x < 0)
    putchar('-');
  x = abs(x);
  print(x);
  putchar('\n');
}
template <class T> inline void write(T x) {
  if (x < 0)
    putchar('-');
  x = abs(x);
  print(x);
}
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
// const ll mod = 10000019;
// const ll phi_mod_prime=mod-1;
ll ksm(ll a, ll b, ll mod) {
  ll ans = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      ans = ans * a % mod;
    a = a * a % mod;
  }
  return ans;
}
ll phi(ll n) {
  ll rea = n;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      rea = rea - rea / i;
      while (n % i == 0)
        n /= i;
    }
  }
  if (n > 1)
    rea = rea - rea / n;
  return rea;
}
// a^(b%modhi(mod)+phi(mod))%mod;
void exgcd(int a, int b, int &x, int &y, int &d) {
  if (!b) {
    x = 1;
    y = 0;
    d = a;
  } else
    exgcd(b, a % b, y, x, d), y -= a / b * x;
}
inline int inv(int a, int mod) {
  int x, y, d;
  exgcd(a, mod, x, y, d);
  x = (x % mod + mod) % mod;
  return x;
}
/********************************Head----Template**********************************************/

const int INF = 0x3f3f3f3f;
const int maxn = 1000 + 5;

const int mod = 1000000007;

int n, m;
ll dp[maxn][maxn];
int a[maxn], b[maxn];

int main() {
  while (~scanf("%d%d", &n, &m)) {
    memset(dp, 0, sizeof(dp));
    for (int i = 1; i <= n; i++)
      scanf("%d", &a[i]);
    for (int i = 1; i <= m; i++)
      scanf("%d", &b[i]);

    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        if (a[i] == b[j])
          dp[i][j] = dp[i - 1][j] + dp[i][j - 1] + 1;
        else
          dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1];
        dp[i][j] = (dp[i][j] + mod) % mod;
      }
    }
    printf("%lld\n", (dp[n][m] + 1) % mod);
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define lowbit(x) (x & (-x))
#define ll long long
#define ull unsigned long long
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a > b ? b : a)
#define PI acos(-1.0)
#define fir first
#define sec second
#define SZ(x) (x.size())
#define inf 0x3f3f3f3f
#define PB push_back
#define MP make_pair
#define all(x) (x).begin(), (x).end()
#define PII pair<int, int>
#define E 2.7182818284590452353602874713527
using namespace std;

/**********************************************Head-----Template****************************************/
bool Finish_read;
template <class T> inline void read(T &x) {
  Finish_read = 0;
  x = 0;
  int f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    if (ch == EOF)
      return;
    ch = getchar();
  }
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  x *= f;
  Finish_read = 1;
}
template <class T> inline void print(T x) {
  if (x / 10 != 0)
    print(x / 10);
  putchar(x % 10 + '0');
}
template <class T> inline void writeln(T x) {
  if (x < 0)
    putchar('-');
  x = abs(x);
  print(x);
  putchar('\n');
}
template <class T> inline void write(T x) {
  if (x < 0)
    putchar('-');
  x = abs(x);
  print(x);
}
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
// const ll mod = 10000019;
// const ll phi_mod_prime=mod-1;
ll ksm(ll a, ll b, ll mod) {
  ll ans = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      ans = ans * a % mod;
    a = a * a % mod;
  }
  return ans;
}
ll phi(ll n) {
  ll rea = n;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      rea = rea - rea / i;
      while (n % i == 0)
        n /= i;
    }
  }
  if (n > 1)
    rea = rea - rea / n;
  return rea;
}
// a^(b%modhi(mod)+phi(mod))%mod;
void exgcd(int a, int b, int &x, int &y, int &d) {
  if (!b) {
    x = 1;
    y = 0;
    d = a;
  } else
    exgcd(b, a % b, y, x, d), y -= a / b * x;
}
inline int inv(int a, int mod) {
  int x, y, d;
  exgcd(a, mod, x, y, d);
  x = (x % mod + mod) % mod;
  return x;
}
/********************************Head----Template**********************************************/

const int INF = 0x3f3f3f3f;
const int maxn = 2000 + 7;

const int mod = 1000000007;

int n, m;
ll dp[maxn][maxn];
int a[maxn], b[maxn];

int main() {
  while (~scanf("%d%d", &n, &m)) {
    memset(dp, 0, sizeof(dp));
    for (int i = 1; i <= n; i++)
      scanf("%d", &a[i]);
    for (int i = 1; i <= m; i++)
      scanf("%d", &b[i]);

    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        if (a[i] == b[j])
          dp[i][j] = dp[i - 1][j] + dp[i][j - 1] + 1;
        else
          dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1];
        dp[i][j] = (dp[i][j] + mod) % mod;
      }
    }
    printf("%lld\n", (dp[n][m] + 1) % mod);
  }
  return 0;
}