Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define bug1(x)                                                                \
  { cerr << (#x) << "=" << x << endl; }
#define bug2(x, y)                                                             \
  { cerr << (#x) << "=" << (x) << "    " << (#y) << "=" << (y) << endl; }
#define bug3(x, y, z)                                                          \
  {                                                                            \
    cerr << (#x) << "=" << (x) << "    " << (#y) << "=" << (y) << "    "       \
         << (#z) << "=" << (z) << endl;                                        \
  }
#define bug4(x, y, z, w)                                                       \
  {                                                                            \
    cerr << (#x) << "=" << (x) << "    " << (#y) << "=" << (y) << "    "       \
         << (#z) << "=" << (z) << "    " << (#w) << "=" << w << endl;          \
  }
#define bug5(x, y, z, w, p)                                                    \
  {                                                                            \
    cerr << (#x) << "=" << (x) << "    " << (#y) << "=" << (y) << "    "       \
         << (#z) << "=" << (z) << "    " << (#w) << "=" << w << "    " << (#p) \
         << "=" << p << endl;                                                  \
  }
#define bug6(x, y, z, w, p, q)                                                 \
  {                                                                            \
    cerr << (#x) << "=" << (x) << "    " << (#y) << "=" << (y) << "    "       \
         << (#z) << "=" << (z) << "    " << (#w) << "=" << w << "    " << (#p) \
         << "=" << p << "    " << (#q) << "=" << q << endl;                    \
  }
#define bugn(x, n)                                                             \
  {                                                                            \
    cerr << (#x) << ":";                                                       \
    for (int i = 0; i < n; i++)                                                \
      cerr << x[i] << "  ";                                                    \
    cerr << endl;                                                              \
  }
#define bugnm(x, n, m)                                                         \
  {                                                                            \
    cerr << (#x) << endl;                                                      \
    for (int i = 0; i < n; i++) {                                              \
      cerr << "Row #" << i << ":";                                             \
      for (int j = 0; j < m; j++)                                              \
        cerr << x[i][j] << "   ";                                              \
      cerr << endl;                                                            \
    }                                                                          \
  }
typedef long long ll;
typedef long double ld;
using namespace std;

#define next _
const int maxn = 1e5 + 10, mod = 1e9 + 7;
vector<int> a(maxn), dp(maxn), next(maxn), mp(maxn);
int n;
int32_t main() {
  IOS cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = n; i >= 1; i--) {
    if (mp[a[i]]) {
      next[i] = mp[a[i]];
    }
    mp[a[i]] = i;
  }
  dp[n] = 1;
  for (int i = n - 1; i >= 1; i--) {
    dp[i] += dp[i + 1];
    if (next[i] && next[i] != i + 1) {
      dp[i] += dp[next[i]];
    }
    dp[i] %= mod;
  }
  cout << dp[1];
}
/*
 * long long or int?
 * index out of bound?
 * Tested on own test case?corner?
 * Make more general solution.
 * Read Read Read Read ....
 */


Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define bug1(x)                                                                \
  { cerr << (#x) << "=" << x << endl; }
#define bug2(x, y)                                                             \
  { cerr << (#x) << "=" << (x) << "    " << (#y) << "=" << (y) << endl; }
#define bug3(x, y, z)                                                          \
  {                                                                            \
    cerr << (#x) << "=" << (x) << "    " << (#y) << "=" << (y) << "    "       \
         << (#z) << "=" << (z) << endl;                                        \
  }
#define bug4(x, y, z, w)                                                       \
  {                                                                            \
    cerr << (#x) << "=" << (x) << "    " << (#y) << "=" << (y) << "    "       \
         << (#z) << "=" << (z) << "    " << (#w) << "=" << w << endl;          \
  }
#define bug5(x, y, z, w, p)                                                    \
  {                                                                            \
    cerr << (#x) << "=" << (x) << "    " << (#y) << "=" << (y) << "    "       \
         << (#z) << "=" << (z) << "    " << (#w) << "=" << w << "    " << (#p) \
         << "=" << p << endl;                                                  \
  }
#define bug6(x, y, z, w, p, q)                                                 \
  {                                                                            \
    cerr << (#x) << "=" << (x) << "    " << (#y) << "=" << (y) << "    "       \
         << (#z) << "=" << (z) << "    " << (#w) << "=" << w << "    " << (#p) \
         << "=" << p << "    " << (#q) << "=" << q << endl;                    \
  }
#define bugn(x, n)                                                             \
  {                                                                            \
    cerr << (#x) << ":";                                                       \
    for (int i = 0; i < n; i++)                                                \
      cerr << x[i] << "  ";                                                    \
    cerr << endl;                                                              \
  }
#define bugnm(x, n, m)                                                         \
  {                                                                            \
    cerr << (#x) << endl;                                                      \
    for (int i = 0; i < n; i++) {                                              \
      cerr << "Row #" << i << ":";                                             \
      for (int j = 0; j < m; j++)                                              \
        cerr << x[i][j] << "   ";                                              \
      cerr << endl;                                                            \
    }                                                                          \
  }
typedef long long ll;
typedef long double ld;
using namespace std;

#define next _
const int maxn = 2e5 + 10, mod = 1e9 + 7;
vector<int> a(maxn), dp(maxn), next(maxn), mp(maxn);
int n;
int32_t main() {
  IOS cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = n; i >= 1; i--) {
    if (mp[a[i]]) {
      next[i] = mp[a[i]];
    }
    mp[a[i]] = i;
  }
  dp[n] = 1;
  for (int i = n - 1; i >= 1; i--) {
    dp[i] += dp[i + 1];
    if (next[i] && next[i] != i + 1) {
      dp[i] += dp[next[i]];
    }
    dp[i] %= mod;
  }
  cout << dp[1];
}
/*
 * long long or int?
 * index out of bound?
 * Tested on own test case?corner?
 * Make more general solution.
 * Read Read Read Read ....
 */