prithivMLmods/Coder-Stat huggingface dataset:
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;

const int MOD = 1000000007;
int dp[100010];
vector<int> before(100010, -1); // ある色の直前の位置

int main() {
  int n;
  cin >> n;
  vector<int> lc(n);
  for (int i = 0; i < n; ++i) {
    cin >> lc[i];
  }
  dp[0] = 1;
  for (int i = 0; i < n; ++i) {
    int c = lc[i];
    int bi = before[c];
    // cout << i << " " << lc[i] << " " << bi << endl;
    dp[i + 1] = dp[i]; // 反転させない場合の数
    before[c] = i;
    if (bi == -1 || bi == i - 1)
      continue;
    dp[i + 1] += dp[bi + 1]; // 反転させる場合の数
    dp[i + 1] %= MOD;
  }
  cout << dp[n] << endl;
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;

const int MOD = 1000000007;
int dp[200010];
vector<int> before(200010, -1); // ある色の直前の位置

int main() {
  int n;
  cin >> n;
  vector<int> lc(n);
  for (int i = 0; i < n; ++i) {
    cin >> lc[i];
  }
  dp[0] = 1;
  for (int i = 0; i < n; ++i) {
    int c = lc[i];
    int bi = before[c];
    // cout << i << " " << lc[i] << " " << bi << endl;
    dp[i + 1] = dp[i]; // 反転させない場合の数
    before[c] = i;
    if (bi == -1 || bi == i - 1)
      continue;
    dp[i + 1] += dp[bi + 1]; // 反転させる場合の数
    dp[i + 1] %= MOD;
  }
  cout << dp[n] << endl;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;

const ll MOD = 1000000007;

int main() {
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  vector<int> a;
  for (int i = 0; i < n; ++i) {
    int c;
    cin >> c;
    --c;
    if (a.size() == 0)
      a.push_back(c);
    else if (a.back() != c)
      a.push_back(c);
  }
  n = a.size();
  vector<ll> dp(n);
  vector<ll> przed(100000, 0);
  for (int i = 0; i < n; ++i) {
    if (przed[a[i]] == 0) {
      if (i != 0)
        dp[i] = dp[i - 1];
      else
        dp[i] = 1;
    } else
      dp[i] = (przed[a[i]] + dp[i - 1]) % MOD;
    if (i != 0)
      przed[a[i]] = (przed[a[i]] + dp[i - 1]) % MOD;
    else
      przed[a[i]] = 1;
  }
  cout << dp[n - 1];
  return 0;
}


Fixed submission:
#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;

const ll MOD = 1000000007;

int main() {
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  vector<int> a;
  for (int i = 0; i < n; ++i) {
    int c;
    cin >> c;
    --c;
    if (a.size() == 0)
      a.push_back(c);
    else if (a.back() != c)
      a.push_back(c);
  }
  n = a.size();
  vector<ll> dp(n);
  vector<ll> przed(200000, 0);
  for (int i = 0; i < n; ++i) {
    if (przed[a[i]] == 0) {
      if (i != 0)
        dp[i] = dp[i - 1];
      else
        dp[i] = 1;
    } else
      dp[i] = (przed[a[i]] + dp[i - 1]) % MOD;
    if (i != 0)
      przed[a[i]] = (przed[a[i]] + dp[i - 1]) % MOD;
    else
      przed[a[i]] = 1;
  }
  cout << dp[n - 1];
  return 0;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>

using namespace std;

const int mod = 1e9 + 7;

int n, c[100010], lst[100010], f[100010];

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &c[i]);

  f[0] = 1;
  for (int i = 1; i <= n; ++i) {
    f[i] = f[i - 1];
    if (lst[c[i]] && lst[c[i]] != i - 1)
      (f[i] += f[lst[c[i]]]) %= mod;
    lst[c[i]] = i;
  }

  printf("%d\n", f[n]);

  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>

using namespace std;

const int mod = 1e9 + 7;

int n, c[200010], lst[200010], f[200010];

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &c[i]);

  f[0] = 1;
  for (int i = 1; i <= n; ++i) {
    f[i] = f[i - 1];
    if (lst[c[i]] && lst[c[i]] != i - 1)
      (f[i] += f[lst[c[i]]]) %= mod;
    lst[c[i]] = i;
  }

  printf("%d\n", f[n]);

  return 0;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int md = 1e9 + 7;
inline void add(int &a, int b) {
  a += b;
  if (a >= md)
    a -= md;
}
int main() {
  int n;
  cin >> n;
  vector<int> c(n + 1);
  c[0] = -1;
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
    c[i]--;
  }
  vector<int> dp(n + 1, 0), ep(n, 0);
  dp[0] = 1;
  for (int i = 1; i <= n; i++) {
    int prv = c[i - 1];
    int crr = c[i];
    if (prv != crr)
      add(ep[crr], dp[i - 1]);
    dp[i] = ep[crr];
  }
  cout << dp[n] << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int md = 1e9 + 7;
inline void add(int &a, int b) {
  a += b;
  if (a >= md)
    a -= md;
}
int main() {
  int n;
  cin >> n;
  vector<int> c(n + 1);
  c[0] = -1;
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
    c[i]--;
  }
  vector<int> dp(n + 1, 0), ep(2e5, 0);
  dp[0] = 1;
  for (int i = 1; i <= n; i++) {
    int prv = c[i - 1];
    int crr = c[i];
    if (prv != crr)
      add(ep[crr], dp[i - 1]);
    dp[i] = ep[crr];
  }
  cout << dp[n] << '\n';
  return 0;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
/*  -*- coding: utf-8 -*-
 *
 * b.cc: B: Reversi
 */

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_C = 100000;
const int MOD = 1000000007;

/* typedef */

/* global variables */

int cs[MAX_N], nxts[MAX_N], ps[MAX_C], dp[MAX_N + 1];

/* subroutines */

inline void addmod(int &a, int b) { a = (a + b) % MOD; }

/* main */

int main() {
  int n;
  scanf("%d", &n);

  for (int i = 0; i < n; i++)
    scanf("%d", cs + i), cs[i]--;

  memset(nxts, -1, sizeof(nxts));
  memset(ps, -1, sizeof(ps));

  for (int i = n - 1; i >= 0; i--) {
    int &pci = ps[cs[i]];
    if (pci >= 0)
      nxts[i] = pci;
    pci = i;
  }

  dp[0] = 1;

  for (int i = 0; i < n; i++) {
    addmod(dp[i + 1], dp[i]);
    if (nxts[i] > i + 1)
      addmod(dp[nxts[i]], dp[i]);
  }

  printf("%d\n", dp[n]);
  return 0;
}


Fixed submission:
/*  -*- coding: utf-8 -*-
 *
 * b.cc: B: Reversi
 */

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 200000;
const int MAX_C = 200000;
const int MOD = 1000000007;

/* typedef */

/* global variables */

int cs[MAX_N], nxts[MAX_N], ps[MAX_C], dp[MAX_N + 1];

/* subroutines */

inline void addmod(int &a, int b) { a = (a + b) % MOD; }

/* main */

int main() {
  int n;
  scanf("%d", &n);

  for (int i = 0; i < n; i++)
    scanf("%d", cs + i), cs[i]--;

  memset(nxts, -1, sizeof(nxts));
  memset(ps, -1, sizeof(ps));

  for (int i = n - 1; i >= 0; i--) {
    int &pci = ps[cs[i]];
    if (pci >= 0)
      nxts[i] = pci;
    pci = i;
  }

  dp[0] = 1;

  for (int i = 0; i < n; i++) {
    addmod(dp[i + 1], dp[i]);
    if (nxts[i] > i + 1)
      addmod(dp[nxts[i]], dp[i]);
  }

  printf("%d\n", dp[n]);
  return 0;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const long long mod = 1000000007LL;

int main(int argc, char const *argv[]) {
  int n;
  cin >> n;
  int c[n];
  for (int i = 0; i < n; ++i) {
    cin >> c[i];
  }
  long long ans[n], memo[100005] = {};
  ans[0] = 1LL;
  memo[c[0]] = 1LL;
  for (int i = 1; i < n; ++i) {
    ans[i] = ans[i - 1];
    if (c[i] != c[i - 1]) {
      ans[i] += memo[c[i]];
    }
    ans[i] %= mod;
    memo[c[i]] = ans[i];
  }
  cout << ans[n - 1] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

const long long mod = 1000000007LL;

int main(int argc, char const *argv[]) {
  int n;
  cin >> n;
  int c[n];
  for (int i = 0; i < n; ++i) {
    cin >> c[i];
  }
  long long ans[n + 5], memo[200005] = {};
  ans[0] = 1LL;
  memo[c[0]] = 1LL;
  for (int i = 1; i < n; ++i) {
    ans[i] = ans[i - 1];
    if (c[i] != c[i - 1]) {
      ans[i] += memo[c[i]];
    }
    ans[i] %= mod;
    memo[c[i]] = ans[i];
  }
  cout << ans[n - 1] << endl;
  return 0;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
typedef long long ll;
int main() {
  ll N;
  cin >> N;
  vector<ll> C;
  for (int i = 0; i < N; i++) {
    int c;
    cin >> c;
    if (i == 0) {
      C.emplace_back(c);
    } else if (C.back() != c) {
      C.emplace_back(c);
    }
  }
  vector<ll> dp(200010, 0LL);
  dp[0] = 1LL;
  vector<ll> num(N + 1, 0);
  for (int i = 0; i < C.size(); i++) {
    dp[i] += num[C[i]];
    dp[i] %= mod;
    if (i == 0) {
      num[C[i]] = 1LL;
    } else {
      num[C[i]] += dp[i - 1];
    }
    if (i > 0)
      dp[i] += dp[i - 1];
    dp[i] %= mod;
  }
  cout << dp[C.size() - 1] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
typedef long long ll;
int main() {
  ll N;
  cin >> N;
  vector<ll> C;
  for (int i = 0; i < N; i++) {
    int c;
    cin >> c;
    if (i == 0) {
      C.emplace_back(c);
    } else if (C.back() != c) {
      C.emplace_back(c);
    }
  }
  vector<ll> dp(200010, 0LL);
  dp[0] = 1LL;
  vector<ll> num(200010, 0);
  for (int i = 0; i < C.size(); i++) {
    dp[i] += num[C[i]];
    dp[i] %= mod;
    if (i == 0) {
      num[C[i]] = 1LL;
    } else {
      num[C[i]] += dp[i - 1];
    }
    if (i > 0)
      dp[i] += dp[i - 1];
    dp[i] %= mod;
  }
  cout << dp[C.size() - 1] << endl;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = long long;
using std::cin;
using std::cout;
using std::endl;

int main() {
  int n;
  scanf("%d", &n);
  std::vector<int> c(n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &c[i]);

    c[i]--;
  }

  const int M = 2e5 + 10;
  std::vector<int> latte(n, -1);
  std::vector<int> vec(M, -1);
  for (int i = 0; i < n; i++) {
    if (latte[c[i]] != -1 and latte[c[i]] + 1 != i) {
      vec[latte[c[i]]] = i;
    }

    latte[c[i]] = i;
  }

  const int MOD = 1e9 + 7;
  std::vector<int> dp(n + 1, 0);
  dp[0] = 1;
  for (int i = 0; i < n; i++) {
    (dp[i + 1] += dp[i]) %= MOD;

    if (vec[i] != -1)
      (dp[vec[i]] += dp[i]) %= MOD;
  }
  printf("%d\n", dp[n]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = long long;
using std::cin;
using std::cout;
using std::endl;

int main() {
  int n;
  scanf("%d", &n);
  std::vector<int> c(n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &c[i]);

    c[i]--;
  }

  const int M = 2e5 + 10;
  std::vector<int> latte(M, -1);
  std::vector<int> vec(n, -1);
  for (int i = 0; i < n; i++) {
    if (latte[c[i]] != -1 and latte[c[i]] + 1 != i) {
      vec[latte[c[i]]] = i;
    }

    latte[c[i]] = i;
  }

  const int MOD = 1e9 + 7;
  std::vector<int> dp(n + 1, 0);
  dp[0] = 1;
  for (int i = 0; i < n; i++) {
    (dp[i + 1] += dp[i]) %= MOD;

    if (vec[i] != -1)
      (dp[vec[i]] += dp[i]) %= MOD;
  }
  printf("%d\n", dp[n]);
  return 0;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;

int N;
int mod = 1e9 + 7;

int main() {
  cin >> N;
  vector<long long> dp(N + 1, 1);
  vector<int> p(N + 1, -1); // p[i]: 最後の色iの位置
  int c;
  cin >> c;
  p[c] = 1;
  dp[0] = 1;
  for (int i = 1; i < N; i++) {
    cin >> c;
    dp[i] = dp[i - 1];
    if (p[c] > 0 && p[c] != i) {
      dp[i] += dp[p[c] - 1];
      dp[i] %= mod;
    }
    p[c] = i + 1;
  }
  cout << dp[N - 1] << endl;
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;

int N;
int mod = 1e9 + 7;

int main() {
  cin >> N;
  vector<long long> dp(N + 1, 1);
  vector<int> p(2e5 + 1, -1); // p[i]: 最後の色iの位置
  int c;
  cin >> c;
  p[c] = 1;
  dp[0] = 1;
  for (int i = 1; i < N; i++) {
    cin >> c;
    dp[i] = dp[i - 1];
    if (p[c] > 0 && p[c] != i) {
      dp[i] += dp[p[c] - 1];
      dp[i] %= mod;
    }
    p[c] = i + 1;
  }
  cout << dp[N - 1] << endl;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define M 100005
using namespace std;
const int mod = 1e9 + 7;
int n, c[M];
int dp[M], val[M];
void Add(int &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &c[i]);
  dp[0] = 1;
  c[0] = -1;
  for (int i = 1; i <= n; i++) {
    if (c[i] != c[i - 1])
      Add(val[c[i]], dp[i - 1]);
    Add(dp[i], val[c[i]]);
  }
  printf("%d\n", dp[n]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define M 200005
using namespace std;
const int mod = 1e9 + 7;
int n, c[M];
int dp[M], val[M];
void Add(int &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &c[i]);
  dp[0] = 1;
  c[0] = -1;
  for (int i = 1; i <= n; i++) {
    if (c[i] != c[i - 1])
      Add(val[c[i]], dp[i - 1]);
    Add(dp[i], val[c[i]]);
  }
  printf("%d\n", dp[n]);
  return 0;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define x first
#define y second
#define bug(x) cerr << #x << '=' << x << ' '
#define debug(x) cerr << #x << '=' << x << '\n'
#define FOR(a, b, c) for (int a = (b), a##_end = (c); a <= a##_end; ++a)
#define ROF(a, b, c) for (int a = (b), a##_end = (c); a >= a##_end; --a)
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int INF = 0x3f3f3f3f, N = 1e5 + 10, P = 1e9 + 7;
template <class T> inline bool chkmin(T &A, const T &B) {
  return B < A ? A = B, 1 : 0;
}
template <class T> inline bool chkmax(T &A, const T &B) {
  return A < B ? A = B, 1 : 0;
}
int dp[N], nxt[N], n;
int now[N], A[N];
int main() {
  scanf("%d", &n);
  FOR(i, 1, n) scanf("%d", &A[i]);
  ROF(i, n, 1) {
    nxt[i] = now[A[i]];
    now[A[i]] = i;
  }
  dp[1] = 1;
  FOR(i, 1, n - 1) {
    if (nxt[i] != i + 1)
      dp[nxt[i]] = (dp[nxt[i]] + dp[i]) % P;
    dp[i + 1] = (dp[i + 1] + dp[i]) % P;
  }
  printf("%d\n", dp[n]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define x first
#define y second
#define bug(x) cerr << #x << '=' << x << ' '
#define debug(x) cerr << #x << '=' << x << '\n'
#define FOR(a, b, c) for (int a = (b), a##_end = (c); a <= a##_end; ++a)
#define ROF(a, b, c) for (int a = (b), a##_end = (c); a >= a##_end; --a)
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int INF = 0x3f3f3f3f, N = 2e5 + 10, P = 1e9 + 7;
template <class T> inline bool chkmin(T &A, const T &B) {
  return B < A ? A = B, 1 : 0;
}
template <class T> inline bool chkmax(T &A, const T &B) {
  return A < B ? A = B, 1 : 0;
}
int dp[N], nxt[N], n;
int now[N], A[N];
int main() {
  scanf("%d", &n);
  FOR(i, 1, n) scanf("%d", &A[i]);
  ROF(i, n, 1) {
    nxt[i] = now[A[i]];
    now[A[i]] = i;
  }
  dp[1] = 1;
  FOR(i, 1, n - 1) {
    if (nxt[i] != i + 1)
      dp[nxt[i]] = (dp[nxt[i]] + dp[i]) % P;
    dp[i + 1] = (dp[i + 1] + dp[i]) % P;
  }
  printf("%d\n", dp[n]);
  return 0;
}
Problem p03096 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
using ll = int64_t;

constexpr ll MOD = 1e9 + 7;
vector<ll> pos[200001];
ll N;
vector<ll> C;
vector<ll> memo;

ll solve(ll left) {
  if (left >= N) {
    return 1;
  }

  if (memo[left] != -1) {
    return memo[left];
  }

  ll next = *upper_bound(pos[C[left]].begin(), pos[C[left]].end(), left);

  ll ans;

  if (next == left + 1) {
    ans = solve(next);
  } else if (next >= N) {
    ans = solve(left + 1);
  } else {
    ans = (solve(next) + solve(left + 1)) % MOD;
  }

  return ans;
}

int main() {
  cin >> N;
  C.resize(N);
  memo.resize(N, -1);
  for (ll i = 0; i < N; i++) {
    cin >> C[i];
    pos[C[i]].push_back(i);
  }
  for (ll i = 1; i <= 200000; i++) {
    pos[i].push_back(N);
  }

  cout << solve(0) << endl;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
using ll = int64_t;

constexpr ll MOD = 1e9 + 7;
vector<ll> pos[200001];
ll N;
vector<ll> C;
vector<ll> memo;

ll solve(ll left) {
  if (left >= N) {
    return 1;
  }

  if (memo[left] != -1) {
    return memo[left];
  }

  ll next = *upper_bound(pos[C[left]].begin(), pos[C[left]].end(), left);

  ll ans;

  if (next == left + 1) {
    ans = solve(next);
  } else if (next >= N) {
    ans = solve(left + 1);
  } else {
    ans = (solve(next) + solve(left + 1)) % MOD;
  }

  return memo[left] = ans;
}

int main() {
  cin >> N;
  C.resize(N);
  memo.resize(N, -1);
  for (ll i = 0; i < N; i++) {
    cin >> C[i];
    pos[C[i]].push_back(i);
  }
  for (ll i = 1; i <= 200000; i++) {
    pos[i].push_back(N);
  }

  cout << solve(0) << endl;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long int LL;
typedef pair<int, int> P;
typedef pair<int, pair<int, int>> PP;
typedef pair<LL, int> LP;
const int INF = 1 << 30;
const LL MAX = 1e9 + 7;

void array_show(int *a, int n) {
  for (int i = 0; i < n; i++)
    printf("%d%c", a[i], (i != n - 1 ? ' ' : '\n'));
}
void array_show(LL *a, int n) {
  for (int i = 0; i < n; i++)
    printf("%lld%c", a[i], (i != n - 1 ? ' ' : '\n'));
}

vector<int> v1;
LL num[100005];

int main() {
  int n;
  int i, j, k;
  LL a, b, c;
  cin >> n;
  for (i = 0, a = -1; i < n; i++) {
    cin >> b;
    if (a != b)
      v1.push_back(b);
    a = b;
  }
  for (i = 0, a = 1; i < v1.size(); i++, b = a) {
    if (num[v1[i]] == 0) {
      num[v1[i]] = a;
      continue;
    }
    a += num[v1[i]];
    num[v1[i]] += b;
    if (a >= MAX)
      a %= MAX;
    if (num[v1[i]] >= MAX)
      num[v1[i]] %= MAX;
  }
  cout << a << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long int LL;
typedef pair<int, int> P;
typedef pair<int, pair<int, int>> PP;
typedef pair<LL, int> LP;
const int INF = 1 << 30;
const LL MAX = 1e9 + 7;

void array_show(int *a, int n) {
  for (int i = 0; i < n; i++)
    printf("%d%c", a[i], (i != n - 1 ? ' ' : '\n'));
}
void array_show(LL *a, int n) {
  for (int i = 0; i < n; i++)
    printf("%lld%c", a[i], (i != n - 1 ? ' ' : '\n'));
}

vector<int> v1;
LL num[200005];

int main() {
  int n;
  int i, j, k;
  LL a, b, c;
  cin >> n;
  for (i = 0, a = -1; i < n; i++) {
    cin >> b;
    if (a != b)
      v1.push_back(b);
    a = b;
  }
  for (i = 0, a = 1; i < v1.size(); i++, b = a) {
    if (num[v1[i]] == 0) {
      num[v1[i]] = a;
      continue;
    }
    a += num[v1[i]];
    num[v1[i]] += b;
    if (a >= MAX)
      a %= MAX;
    if (num[v1[i]] >= MAX)
      num[v1[i]] %= MAX;
  }
  cout << a << endl;
  return 0;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;

int main() {
  int n;
  cin >> n;
  vector<int> arr;
  int pre = 0;
  for (int i = 0; i < n; i++) {
    int c;
    cin >> c;
    if (pre != c) {
      arr.push_back(c);
      pre = c;
    }
  }
  n = arr.size();
  vector<int> next(n, 0);
  vector<int> last(n + 1, -1);
  for (int i = 0; i < n; i++) {
    if (last[arr[i]] != -1) {
      next[last[arr[i]]] = i;
    }
    last[arr[i]] = i;
  }
  vector<ll> dp(n);
  dp[0] = 1;
  for (int i = 0; i < n - 1; i++) {
    dp[i + 1] = (dp[i + 1] + dp[i]) % MOD;
    if (next[i] > 0)
      dp[next[i]] = (dp[next[i]] + dp[i]) % MOD;
  }
  cout << dp[n - 1] << endl;
}


Fixed submission:
#include <iostream>
#include <vector>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;

int main() {
  int n;
  cin >> n;
  vector<int> arr;
  int pre = 0;
  for (int i = 0; i < n; i++) {
    int c;
    cin >> c;
    if (pre != c) {
      arr.push_back(c);
      pre = c;
    }
  }
  n = arr.size();
  vector<int> next(n, 0);
  vector<int> last(200001, -1);
  for (int i = 0; i < n; i++) {
    if (last[arr[i]] != -1) {
      next[last[arr[i]]] = i;
    }
    last[arr[i]] = i;
  }
  vector<ll> dp(n);
  dp[0] = 1;
  for (int i = 0; i < n - 1; i++) {
    dp[i + 1] = (dp[i + 1] + dp[i]) % MOD;
    if (next[i] > 0)
      dp[next[i]] = (dp[next[i]] + dp[i]) % MOD;
  }
  cout << dp[n - 1] << endl;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <stdio.h>
// #include <bits/stdc++.h>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <float.h>
#include <iomanip>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define INF 1e18
#define rep(i, n) for (int i = 0; (i) < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); (i) <= (int)(b); i++)
#define VEC(type, c, n)                                                        \
  std::vector<type> c(n);                                                      \
  for (auto &i : c)                                                            \
    std::cin >> i;
#define vec(type, n) vector<type>(n)
#define vvec(m, n) vector<vector<int>>(int(m), vector<int>(n))
#define ALL(a) (a).begin(), (a).end()

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using P = pair<ll, ll>;

const int MOD = 1e9 + 7;

ll dp[200010];
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  vector<int> tab(n + 1, -1);
  dp[0] = 1;
  rep(i, n) {
    if (tab[a[i]] == -1 || tab[a[i]] == i) {
      dp[i + 1] = dp[i] % MOD;
    } else {
      dp[i + 1] = (dp[i] + dp[tab[a[i]]]) % MOD;
    }
    tab[a[i]] = i + 1;
  }
  cout << dp[n] << endl;
}


Fixed submission:
#include <iostream>
#include <stdio.h>
// #include <bits/stdc++.h>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <float.h>
#include <iomanip>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define INF 1e18
#define rep(i, n) for (int i = 0; (i) < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); (i) <= (int)(b); i++)
#define VEC(type, c, n)                                                        \
  std::vector<type> c(n);                                                      \
  for (auto &i : c)                                                            \
    std::cin >> i;
#define vec(type, n) vector<type>(n)
#define vvec(m, n) vector<vector<int>>(int(m), vector<int>(n))
#define ALL(a) (a).begin(), (a).end()

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using P = pair<ll, ll>;

const int MOD = 1e9 + 7;

ll dp[200010];
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  vector<int> tab(200010, -1);
  dp[0] = 1;
  rep(i, n) {
    if (tab[a[i]] == -1 || tab[a[i]] == i) {
      dp[i + 1] = dp[i] % MOD;
    } else {
      dp[i + 1] = (dp[i] + dp[tab[a[i]]]) % MOD;
    }
    tab[a[i]] = i + 1;
  }
  cout << dp[n] << endl;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#pragma region
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
// #define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i))
#define rep(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define rrep(i, s) for (int(i) = (s)-1; (i) >= 0; --(i))
#define all(x) x.begin(), x.end()
#pragma endregion

int main() {
  int n;
  cin >> n;
  vector<vector<int>> pos(n + 1);
  vector<ll> dp(n + 1);
  dp[0] = 1;
  int pre = 0;
  rep(i, n) {
    int a;
    cin >> a;
    dp[i + 1] = dp[i];
    if (pre != a && pos[a].size() != 0) {
      dp[i + 1] += dp[pos[a].back() + 1];
    }
    dp[i + 1] %= (ll)1e9 + 7;
    pos[a].push_back(i);
    pre = a;
  }
  cout << dp[n] << endl;
}

Fixed submission:
#pragma region
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
// #define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i))
#define rep(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define rrep(i, s) for (int(i) = (s)-1; (i) >= 0; --(i))
#define all(x) x.begin(), x.end()
#pragma endregion

int main() {
  int n;
  cin >> n;
  vector<vector<int>> pos(200005);
  vector<ll> dp(n + 1);
  dp[0] = 1;
  int pre = 0;
  rep(i, n) {
    int a;
    cin >> a;
    dp[i + 1] = dp[i];
    if (pre != a && pos[a].size() != 0) {
      dp[i + 1] += dp[pos[a].back() + 1];
    }
    dp[i + 1] %= (ll)1e9 + 7;
    pos[a].push_back(i);
    pre = a;
  }
  cout << dp[n] << endl;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  long long md = 1e9 + 7;
  int N;
  cin >> N;
  int bef = -1;
  vector<int> C;
  for (int i = 0; i < N; i++) {
    int c;
    cin >> c;
    if (c != bef)
      C.push_back(c);
    bef = c;
  }
  int Clen = C.size();
  vector<long long> dp(Clen, 0);
  dp[0]++;
  vector<int> check(N + 1, -1);
  for (int i = 0; i < Clen; i++) {
    if (i > 0)
      dp[i] += dp[i - 1];
    if (check[C[i]] >= 0) {
      dp[i] += dp[check[C[i]]];
    }
    dp[i] %= md;
    check[C[i]] = i;
  }
  cout << dp[Clen - 1] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  long long md = 1e9 + 7;
  int N;
  cin >> N;
  int bef = -1;
  vector<int> C;
  for (int i = 0; i < N; i++) {
    int c;
    cin >> c;
    if (c != bef)
      C.push_back(c);
    bef = c;
  }
  int Clen = C.size();
  vector<long long> dp(Clen, 0);
  dp[0]++;
  vector<int> check(2 * 100000 + 1, -1);
  for (int i = 0; i < Clen; i++) {
    if (i > 0)
      dp[i] += dp[i - 1];
    if (check[C[i]] >= 0) {
      dp[i] += dp[check[C[i]]];
    }
    dp[i] %= md;
    check[C[i]] = i;
  }
  cout << dp[Clen - 1] << endl;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const ll mod = 1000000007;
const ll INF = 1001001001;
const ll LINF = 1001001001001001001;
ll n;
vector<ll> c(0);
vector<ll> mem;
vector<vector<ll>> place(200000, vector<ll>(0));

void prvec(vector<ll> vec) { // for debug
  ll n = vec.size();
  cout << "------------------------------------\n";
  rep(i, n) cout << i << " " << vec.at(i) << "\n";
  cout << "------------------------------------\n";
}

void pr2d(vector<vector<ll>> vec) { // for debug
  ll h = vec.size();
  ll w = vec.at(0).size();
  cout << "------------------------------------\n";
  rep(i, h) {
    rep(j, w) { cout << vec.at(i).at(j) << " "; }
    cout << "\n";
  }
  cout << "------------------------------------\n";
}

ll f(ll x) {
  if (x == n - 1)
    return 1;
  if (mem.at(x) != -1)
    return mem.at(x);
  ll ret = f(x + 1);
  ll y = c.at(x);
  ll add = 0;
  auto it = upper_bound(place.at(y).begin(), place.at(y).end(), x);
  if (it != place.at(y).end())
    add = f(*it);
  ret = (ret + add) % mod;
  mem.at(x) = ret;
  return ret;
}

// main starts here

int main() {
  cin >> n;
  ll prev = INF;
  rep(i, n) {
    ll a;
    cin >> a;
    if (a != prev)
      c.emplace_back(a);
    prev = a;
  }
  n = c.size();
  rep(i, n) place.at(c.at(i)).emplace_back(i);
  mem.resize(n, -1);
  cout << f(0) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const ll mod = 1000000007;
const ll INF = 1001001001;
const ll LINF = 1001001001001001001;
ll n;
vector<ll> c(0);
vector<ll> mem;
vector<vector<ll>> place(200001, vector<ll>(0));

void prvec(vector<ll> vec) { // for debug
  ll n = vec.size();
  cout << "------------------------------------\n";
  rep(i, n) cout << i << " " << vec.at(i) << "\n";
  cout << "------------------------------------\n";
}

void pr2d(vector<vector<ll>> vec) { // for debug
  ll h = vec.size();
  ll w = vec.at(0).size();
  cout << "------------------------------------\n";
  rep(i, h) {
    rep(j, w) { cout << vec.at(i).at(j) << " "; }
    cout << "\n";
  }
  cout << "------------------------------------\n";
}

ll f(ll x) {
  if (x == n - 1)
    return 1;
  if (mem.at(x) != -1)
    return mem.at(x);
  ll ret = f(x + 1);
  ll y = c.at(x);
  ll add = 0;
  auto it = upper_bound(place.at(y).begin(), place.at(y).end(), x);
  if (it != place.at(y).end())
    add = f(*it);
  ret = (ret + add) % mod;
  mem.at(x) = ret;
  return ret;
}

// main starts here

int main() {
  cin >> n;
  ll prev = INF;
  rep(i, n) {
    ll a;
    cin >> a;
    if (a != prev)
      c.emplace_back(a);
    prev = a;
  }
  n = c.size();
  rep(i, n) place.at(c.at(i)).emplace_back(i);
  mem.resize(n, -1);
  cout << f(0) << endl;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
using namespace std;

const ll mod = 1e9 + 7;
const int maxn = 5e5 + 7;
int sum[maxn];
stack<int> sta;

std::vector<int> v;

int a[maxn];

int pre[maxn], pos[maxn];
ll dp[maxn];

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n * 10; i++)
    pre[i] = 0;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  stack<int> sta;
  for (int i = 1; i <= n; i++) {
    if (sta.empty())
      sta.push(a[i]);
    else if (sta.top() != a[i])
      sta.push(a[i]);
  }
  int sz = sta.size();
  for (int i = sz; i >= 1; i--)
    a[i] = sta.top(), sta.pop();
  for (int i = 1; i <= sz; i++)
    pre[i] = pos[a[i]], pos[a[i]] = i;
  dp[0] = 1;

  for (int i = 1; i <= sz; i++) {
    dp[i] = dp[i - 1];
    if (pre[i] != 0)
      dp[i] = (dp[i] + dp[pre[i]]) % mod;
  }
  cout << dp[sz] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;

const ll mod = 1e9 + 7;
const int maxn = 5e5 + 7;
int sum[maxn];
stack<int> sta;

std::vector<int> v;

int a[maxn];

int pre[maxn], pos[maxn];
ll dp[maxn];

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n * 2; i++)
    pre[i] = 0;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  stack<int> sta;
  for (int i = 1; i <= n; i++) {
    if (sta.empty())
      sta.push(a[i]);
    else if (sta.top() != a[i])
      sta.push(a[i]);
  }
  int sz = sta.size();
  for (int i = sz; i >= 1; i--)
    a[i] = sta.top(), sta.pop();
  for (int i = 1; i <= sz; i++)
    pre[i] = pos[a[i]], pos[a[i]] = i;
  dp[0] = 1;

  for (int i = 1; i <= sz; i++) {
    dp[i] = dp[i - 1];
    if (pre[i] != 0)
      dp[i] = (dp[i] + dp[pre[i]]) % mod;
  }
  cout << dp[sz] << endl;
  return 0;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <map>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#define MOD 1000000007
#define N_MAX 100005

using namespace std;
typedef long long ll;

int main() {
  int N;
  int C[N_MAX];
  ll dp[N_MAX];
  int m[N_MAX];
  C[0] = 200005;
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> C[i];
    m[C[i]] = 0;
  }
  bool update = false;
  dp[0] = 1;
  for (int i = 1; i <= N; i++) {
    dp[i] = dp[i - 1];
    if (C[i - 1] != C[i]) {
      dp[i] += m[C[i]];
      dp[i] = dp[i] % MOD;
    }
    m[C[i]] = dp[i];
  }
  cout << dp[N] << endl;
}

Fixed submission:
#include <iostream>
#include <map>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#define MOD 1000000007
#define N_MAX 200006

using namespace std;
typedef long long ll;

int main() {
  int N;
  int C[N_MAX];
  ll dp[N_MAX];
  int m[N_MAX];
  C[0] = 200005;
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> C[i];
    m[C[i]] = 0;
  }
  bool update = false;
  dp[0] = 1;
  for (int i = 1; i <= N; i++) {
    dp[i] = dp[i - 1];
    if (C[i - 1] != C[i]) {
      dp[i] += m[C[i]];
      dp[i] = dp[i] % MOD;
    }
    m[C[i]] = dp[i];
  }
  cout << dp[N] << endl;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
using namespace std;
const int N = 1e5 + 10;
typedef long long ll;
const ll mod = 1e9 + 7;
ll coldp[N];
ll dp[N];
int c[N];
int n;
int main() {
  dp[0] = 1;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &c[i]);
  for (int i = 1; i <= n; i++) {
    dp[i] = dp[i - 1];
    if ((i - 1) && c[i - 1] != c[i])
      (dp[i] += coldp[c[i]]) %= mod;
    coldp[c[i]] = dp[i];
    //	printf("%lld ",dp[i]);
  }
  printf("%lld", dp[n]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
using namespace std;
const int N = 2 * 1e5 + 10;
typedef long long ll;
const ll mod = 1e9 + 7;
ll coldp[N];
ll dp[N];
int c[N];
int n;
int main() {
  dp[0] = 1;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &c[i]);
  for (int i = 1; i <= n; i++) {
    dp[i] = dp[i - 1];
    if ((i - 1) && c[i - 1] != c[i])
      (dp[i] += coldp[c[i]]) %= mod;
    coldp[c[i]] = dp[i];
    //	printf("%lld ",dp[i]);
  }
  printf("%lld", dp[n]);
  return 0;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int N;

  cin >> N;
  vector<int> C(N + 1);
  vector<int> latest(N + 1, -1);
  vector<int> DP(N + 1, 0);
  for (int i = 1; i <= N; i++) {
    cin >> C[i];
  }

  DP[0] = 1;
  for (int i = 1; i <= N; i++) {
    if (latest[C[i]] >= 0 && latest[C[i]] + 1 != i) {
      DP[i] = DP[i - 1] + DP[latest[C[i]]];
    } else {
      DP[i] = DP[i - 1];
    }
    DP[i] %= (int)(1e9 + 7);
    latest[C[i]] = i;
  }

  cout << DP[N] << endl;

  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int N;

  cin >> N;
  vector<int> C(N + 1);
  vector<int> latest(200002, -1);
  vector<unsigned int> DP(N + 1, 0);
  for (int i = 1; i <= N; i++) {
    cin >> C[i];
  }

  DP[0] = 1;
  for (int i = 1; i <= N; i++) {
    if (latest[C[i]] >= 0 && latest[C[i]] + 1 != i) {
      DP[i] = DP[i - 1] + DP[latest[C[i]]];
    } else {
      DP[i] = DP[i - 1];
    }
    DP[i] %= (int)(1e9 + 7);
    latest[C[i]] = i;
  }

  cout << DP[N] << endl;

  return 0;
}
Problem p03096 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
const int mo = 1e9 + 7;
const int maxn = 200002;

int n, c[maxn];
vector<int> pos[maxn];

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n;
  vector<int> idx(n, -1);

  for (int i = 0; i < n; ++i) {
    cin >> c[i];
    if (i == 0 || c[i] != c[i - 1]) {
      idx[i] = pos[c[i]].size();
      pos[c[i]].push_back(i);
    }
  }

  vector<long long> f(n);
  f[0] = 1;
  for (int i = 1; i < n; ++i) {
    f[i] = f[i - 1];
    if (idx[i] != -1) {
      int cur = c[i];
      for (int j = idx[i] - 1; j >= 0; --j) {
        int t = pos[cur][j];
        // printf("%d %d\n", t, i);
        if (t == 0)
          ++f[i];
        else
          f[i] += f[t - 1];
      }

      f[i] %= mo;
    }
  }

  cout << f[n - 1] << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
const int mo = 1e9 + 7;
const int maxn = 200002;

int n, c[maxn];
vector<int> pos[maxn];

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n;
  vector<int> idx(n, -1);

  for (int i = 0; i < n; ++i) {
    cin >> c[i];
    if (i == 0 || c[i] != c[i - 1]) {
      idx[i] = pos[c[i]].size();
      pos[c[i]].push_back(i);
    }
  }

  vector<long long> f(n);
  f[0] = 1;
  for (int i = 1; i < n; ++i) {
    f[i] = f[i - 1];
    if (idx[i] > 0) {
      int t = pos[c[i]][idx[i] - 1];

      f[i] += f[t];

      f[i] %= mo;
    }
  }

  cout << f[n - 1] << endl;

  return 0;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#define FOR(i, x, y) for (int i = (x), i##END = (y); i <= i##END; ++i)
#define DOR(i, x, y) for (int i = (x), i##END = (y); i >= i##END; --i)
template <typename T, typename _T> inline bool chk_min(T &x, const _T y) {
  return y < x ? x = y, 1 : 0;
}
template <typename T, typename _T> inline bool chk_max(T &x, const _T y) {
  return x < y ? x = y, 1 : 0;
}
typedef long long ll;
const int N = 1e5 + 5;
const int P = 1e9 + 7;
int dp[N], Sum[N];
int a[N];
int n;

int main() {
  scanf("%d", &n);
  FOR(i, 1, n) scanf("%d", &a[i]);
  int m = n;
  n = 1;
  FOR(i, 2, m) if (a[i] != a[n]) a[++n] = a[i];
  dp[0] = 1;
  FOR(i, 1, n) {
    dp[i] = (dp[i - 1] + Sum[a[i]]) % P;
    (Sum[a[i]] += dp[i - 1]) %= P;
  }
  printf("%d\n", dp[n]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#define FOR(i, x, y) for (int i = (x), i##END = (y); i <= i##END; ++i)
#define DOR(i, x, y) for (int i = (x), i##END = (y); i >= i##END; --i)
template <typename T, typename _T> inline bool chk_min(T &x, const _T y) {
  return y < x ? x = y, 1 : 0;
}
template <typename T, typename _T> inline bool chk_max(T &x, const _T y) {
  return x < y ? x = y, 1 : 0;
}
typedef long long ll;
const int N = 2e5 + 5;
const int P = 1e9 + 7;
int dp[N], Sum[N];
int a[N];
int n;

int main() {
  scanf("%d", &n);
  FOR(i, 1, n) scanf("%d", &a[i]);
  int m = n;
  n = 1;
  FOR(i, 2, m) if (a[i] != a[n]) a[++n] = a[i];
  dp[0] = 1;
  FOR(i, 1, n) {
    dp[i] = (dp[i - 1] + Sum[a[i]]) % P;
    (Sum[a[i]] += dp[i - 1]) %= P;
  }
  printf("%d\n", dp[n]);
  return 0;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
#define N 100005
#define mod 1000000007ll
using namespace std;
typedef long long ll;

inline int rd() {
  int x = 0;
  char c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    x = x * 10 + (c ^ 48);
    c = getchar();
  }
  return x;
}

int n, c[N], f[N], pos[N], lst[N];

int main() {
  n = rd();
  f[0] = 1;
  for (int i = 1; i <= n; ++i) {
    c[i] = rd();
    if (lst[c[i]] < i - 1)
      pos[i] = lst[c[i]];
    lst[c[i]] = i;
    f[i] = (f[i - 1] + (pos[i] ? f[pos[i]] : 0)) % mod;
  }
  printf("%d\n", f[n]);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
#define N 200005
#define mod 1000000007ll
using namespace std;
typedef long long ll;

inline int rd() {
  int x = 0;
  char c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    x = x * 10 + (c ^ 48);
    c = getchar();
  }
  return x;
}

int n, c[N], f[N], pos[N], lst[N];

int main() {
  n = rd();
  f[0] = 1;
  for (int i = 1; i <= n; ++i) {
    c[i] = rd();
    if (lst[c[i]] < i - 1)
      pos[i] = lst[c[i]];
    lst[c[i]] = i;
    f[i] = (f[i - 1] + (pos[i] ? f[pos[i]] : 0)) % mod;
  }
  printf("%d\n", f[n]);
  return 0;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fo(i, a, b) for (int i = a; i <= b; ++i)
#define fod(i, a, b) for (int i = a; i >= b; --i)
#define N 100005
#define LL long long
#define mo 1000000007
using namespace std;
LL f[N];
int lst[N], a[N], n;
int main() {
  cin >> n;
  fo(i, 1, n) scanf("%d", &a[i]);
  f[0] = 1;
  fo(i, 1, n) {
    f[i] = f[i - 1];
    if (lst[a[i]] != i - 1 && lst[a[i]] != 0)
      f[i] = (f[i] + f[lst[a[i]]]) % mo;
    lst[a[i]] = i;
  }
  printf("%lld\n", f[n]);
}

Fixed submission:
#include <bits/stdc++.h>
#define fo(i, a, b) for (int i = a; i <= b; ++i)
#define fod(i, a, b) for (int i = a; i >= b; --i)
#define N 300005
#define LL long long
#define mo 1000000007
using namespace std;
LL f[N];
int lst[N], a[N], n;
int main() {
  cin >> n;
  fo(i, 1, n) scanf("%d", &a[i]);
  f[0] = 1;
  fo(i, 1, n) {
    f[i] = f[i - 1];
    if (lst[a[i]] != i - 1 && lst[a[i]] != 0)
      f[i] = (f[i] + f[lst[a[i]]]) % mo;
    lst[a[i]] = i;
  }
  printf("%lld\n", f[n]);
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, st, ed) for (register int i = st, i##end = ed; i <= i##end; ++i)
#define DREP(i, st, ed) for (register int i = st, i##end = ed; i >= i##end; --i)
template <typename T> inline bool chkmin(T &x, T y) {
  return (y < x) ? (x = y, 1) : 0;
}
template <typename T> inline bool chkmax(T &x, T y) {
  return (y > x) ? (x = y, 1) : 0;
}
typedef long long ll;
inline int read() {
  int x;
  char c;
  int f = 1;
  while ((c = getchar()) != '-' && (c > '9' || c < '0'))
    ;
  if (c == '-')
    f = -1, c = getchar();
  x = c ^ '0';
  while ((c = getchar()) >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + (c ^ '0');
  return x * f;
}
inline ll readll() {
  ll x;
  char c;
  int f = 1;
  while ((c = getchar()) != '-' && (c > '9' || c < '0'))
    ;
  if (c == '-')
    f = -1, c = getchar();
  x = c ^ '0';
  while ((c = getchar()) >= '0' && c <= '9')
    x = (x << 1ll) + (x << 3ll) + (c ^ '0');
  return x * f;
}
const int maxn = 1e5 + 10, mod = 1e9 + 7;
inline void add(int &x, int y) {
  x += y;
  x = (x >= mod ? x - mod : x);
}
int dp[maxn];
int main() {
  int n = read();
  dp[0] = 1;
  int lst = 0;
  REP(i, 1, n) {
    int x = read();
    if (x == lst)
      continue;
    int u = dp[0], v = dp[x];
    add(dp[0], v), add(dp[x], u);
    lst = x;
  }
  printf("%d\n", dp[0]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, st, ed) for (register int i = st, i##end = ed; i <= i##end; ++i)
#define DREP(i, st, ed) for (register int i = st, i##end = ed; i >= i##end; --i)
template <typename T> inline bool chkmin(T &x, T y) {
  return (y < x) ? (x = y, 1) : 0;
}
template <typename T> inline bool chkmax(T &x, T y) {
  return (y > x) ? (x = y, 1) : 0;
}
typedef long long ll;
inline int read() {
  int x;
  char c;
  int f = 1;
  while ((c = getchar()) != '-' && (c > '9' || c < '0'))
    ;
  if (c == '-')
    f = -1, c = getchar();
  x = c ^ '0';
  while ((c = getchar()) >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + (c ^ '0');
  return x * f;
}
inline ll readll() {
  ll x;
  char c;
  int f = 1;
  while ((c = getchar()) != '-' && (c > '9' || c < '0'))
    ;
  if (c == '-')
    f = -1, c = getchar();
  x = c ^ '0';
  while ((c = getchar()) >= '0' && c <= '9')
    x = (x << 1ll) + (x << 3ll) + (c ^ '0');
  return x * f;
}
const int maxn = 2e5 + 10, mod = 1e9 + 7;
inline void add(int &x, int y) {
  x += y;
  x = (x >= mod ? x - mod : x);
}
int dp[maxn];
int main() {
  int n = read();
  dp[0] = 1;
  int lst = 0;
  REP(i, 1, n) {
    int x = read();
    if (x == lst)
      continue;
    int u = dp[0], v = dp[x];
    add(dp[0], v), add(dp[x], u);
    lst = x;
  }
  printf("%d\n", dp[0]);
  return 0;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define sz(x) int(x.size())
#define ALL(c) (c).begin(), (c).end()
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
#define FORV(i, v) for (auto i = v.begin(); i != v.end(); i++)
using namespace std;

typedef int64_t ll;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const int INF = 1001001001;
const ll INFL = (1LL << 60);
const double eps = (1e-9);

const ll mod = 1e9 + 7;

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

  int n;
  cin >> n;
  vector<ll> c(n);
  rep(i, n) cin >> c[i];

  vector<ll> sum(n, 0);
  vector<ll> dp(n + 1, 0);
  dp[0] = 1;

  rep(i, n) {
    ll x = c[i];
    if (i != 0 && c[i] == c[i - 1]) {
      dp[i + 1] = dp[i];
      continue; // 同じ値の間には区切りを入れない
    }
    sum[x] = (sum[x] + dp[i]) % mod;
    dp[i + 1] = sum[x];
  }

  cout << dp[n] << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define sz(x) int(x.size())
#define ALL(c) (c).begin(), (c).end()
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
#define FORV(i, v) for (auto i = v.begin(); i != v.end(); i++)
using namespace std;

typedef int64_t ll;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const int INF = 1001001001;
const ll INFL = (1LL << 60);
const double eps = (1e-9);

const ll mod = 1e9 + 7;

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

  int n;
  cin >> n;
  vector<ll> c(n);
  rep(i, n) cin >> c[i];

  vector<ll> sum(2e5 + 5, 0);
  vector<ll> dp(n + 1, 0);
  dp[0] = 1;

  rep(i, n) {
    ll x = c[i];
    if (i != 0 && c[i] == c[i - 1]) {
      dp[i + 1] = dp[i];
      continue; // 同じ値の間には区切りを入れない
    }
    sum[x] = (sum[x] + dp[i]) % mod;
    dp[i + 1] = sum[x];
  }

  cout << dp[n] << endl;

  return 0;
}
Problem p03096 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define LL long long
#define clr(x, y) memset(x, y, sizeof(x))
#define FOR(x, l, r) for (int x = l, x##_ = r; x <= x##_; x++)
#define FR(x, l, r) for (int x = l, x##_ = r; x < x##_; x++)
#define DOR(x, r, l) for (int x = r, x##_ = l; x >= x##_; x--)
using namespace std;
const int N = 200005;
const LL P = 1e9 + 7;
LL dp[N];
int n, A[N], tot, R[N], L[N], top[N];
LL dfs(int x) {
  //	printf("%d\n",x);
  if (x == tot)
    return 1;
  //	if(dp[x])return dp[x];
  LL cnt = dfs(x + 1);
  int to = R[x];
  //	while(to){
  //	printf("R:%d %d\n",to,tot);
  if (to)
    (cnt += dfs(to)) %= P;
  //		to=R[to];
  //	}
  return dp[x] = cnt;
}
void Add(int x, int id) {
  if (top[x])
    R[top[x]] = id, L[id] = top[x];
  top[x] = id;
}
int main() {
  scanf("%d", &n);
  FOR(i, 1, n) {
    int x;
    scanf("%d", &x);
    if (x != A[tot]) {
      A[++tot] = x;
      Add(x, tot);
    }
  }
  dp[tot] = 1;
  printf("%lld\n", dfs(1));
  //	FOR(i,1,n)printf("%lld ",dp[i]);puts("");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define LL long long
#define clr(x, y) memset(x, y, sizeof(x))
#define FOR(x, l, r) for (int x = l, x##_ = r; x <= x##_; x++)
#define FR(x, l, r) for (int x = l, x##_ = r; x < x##_; x++)
#define DOR(x, r, l) for (int x = r, x##_ = l; x >= x##_; x--)
using namespace std;
const int N = 200005;
const LL P = 1e9 + 7;
LL dp[N];
int n, A[N], tot, R[N], L[N], top[N];
LL dfs(int x) {
  //	printf("%d\n",x);
  if (x == tot)
    return 1;
  if (dp[x])
    return dp[x];
  LL cnt = dfs(x + 1);
  int to = R[x];
  //	while(to){
  //	printf("R:%d %d\n",to,tot);
  if (to)
    (cnt += dfs(to)) %= P;
  //		to=R[to];
  //	}
  return dp[x] = cnt;
}
void Add(int x, int id) {
  if (top[x])
    R[top[x]] = id, L[id] = top[x];
  top[x] = id;
}
int main() {
  scanf("%d", &n);
  FOR(i, 1, n) {
    int x;
    scanf("%d", &x);
    if (x != A[tot]) {
      A[++tot] = x;
      Add(x, tot);
    }
  }
  dp[tot] = 1;
  printf("%lld\n", dfs(1));
  //	FOR(i,1,n)printf("%lld ",dp[i]);puts("");
  return 0;
}