Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, n) for (int i = 1; i < (n); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) (int)(x).size()
using namespace std;
using ll = long long;
constexpr int inf = 1000000000;
constexpr ll INF = 1000000000000000000;

constexpr int mod = 1000000007;
struct ModInt {
  ll x;
  ModInt(ll a) : x((a % mod + mod) % mod) {}
  ModInt operator-() const { return ModInt(-x); }
  ModInt &operator+=(const ModInt &a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  ModInt &operator-=(const ModInt &a) {
    if ((x -= a.x) < 0)
      x += mod;
    return *this;
  }
  ModInt &operator*=(const ModInt &a) {
    (x *= a.x) %= mod;
    return *this;
  }
  ModInt operator+(const ModInt &a) const {
    ModInt res(*this);
    return res += a;
  }
  ModInt operator-(const ModInt &a) const {
    ModInt res(*this);
    return res -= a;
  }
  ModInt operator*(const ModInt &a) const {
    ModInt res(*this);
    return res *= a;
  }
  ModInt pow(ll t) const {
    if (!t)
      return 1;
    ModInt a = pow(t / 2);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
  ModInt inv() const { return pow(mod - 2); }
  ModInt &operator/=(const ModInt &a) { return (*this) *= a.inv(); }
  ModInt operator/(const ModInt &a) const {
    ModInt res(*this);
    return res /= a;
  }
  friend ostream &operator<<(ostream &os, const ModInt &a) {
    os << a.x;
    return os;
  }
};

constexpr int c_max = 100010;

int main() {
  int N;
  cin >> N;
  vector<int> C;
  rep(i, N) {
    int c;
    cin >> c;
    if (i) {
      if (c != C.back()) {
        C.push_back(c);
      }
    } else {
      C.push_back(c);
    }
  }
  vector<ModInt> dp;
  dp.push_back(ModInt{1});
  vector<int> MX(c_max, -1);
  MX[C[0]] = 0;
  REP(i, SZ(C)) {
    ModInt res = dp.back();
    if (MX[C[i]] != -1)
      res += dp[MX[C[i]]];
    dp.push_back(res);
    MX[C[i]] = i;
  }
  cout << dp.back() << "\n";
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, n) for (int i = 1; i < (n); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) (int)(x).size()
using namespace std;
using ll = long long;
constexpr int inf = 1000000000;
constexpr ll INF = 1000000000000000000;

constexpr int mod = 1000000007;
struct ModInt {
  ll x;
  ModInt(ll a) : x((a % mod + mod) % mod) {}
  ModInt operator-() const { return ModInt(-x); }
  ModInt &operator+=(const ModInt &a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  ModInt &operator-=(const ModInt &a) {
    if ((x -= a.x) < 0)
      x += mod;
    return *this;
  }
  ModInt &operator*=(const ModInt &a) {
    (x *= a.x) %= mod;
    return *this;
  }
  ModInt operator+(const ModInt &a) const {
    ModInt res(*this);
    return res += a;
  }
  ModInt operator-(const ModInt &a) const {
    ModInt res(*this);
    return res -= a;
  }
  ModInt operator*(const ModInt &a) const {
    ModInt res(*this);
    return res *= a;
  }
  ModInt pow(ll t) const {
    if (!t)
      return 1;
    ModInt a = pow(t / 2);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
  ModInt inv() const { return pow(mod - 2); }
  ModInt &operator/=(const ModInt &a) { return (*this) *= a.inv(); }
  ModInt operator/(const ModInt &a) const {
    ModInt res(*this);
    return res /= a;
  }
  friend ostream &operator<<(ostream &os, const ModInt &a) {
    os << a.x;
    return os;
  }
};

constexpr int c_max = 200010;

int main() {
  int N;
  cin >> N;
  vector<int> C;
  rep(i, N) {
    int c;
    cin >> c;
    if (i) {
      if (c != C.back()) {
        C.push_back(c);
      }
    } else {
      C.push_back(c);
    }
  }
  vector<ModInt> dp;
  dp.push_back(ModInt{1});
  vector<int> MX(c_max, -1);
  MX[C[0]] = 0;
  REP(i, SZ(C)) {
    ModInt res = dp.back();
    if (MX[C[i]] != -1)
      res += dp[MX[C[i]]];
    dp.push_back(res);
    MX[C[i]] = i;
  }
  cout << dp.back() << "\n";
  return 0;
}