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

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

const int mod = 1e9 + 7;

int main() {
  ll n;
  cin >> n;
  vector<ll> c(1);
  cin >> c[0];
  for (ll i = 1; i < n; ++i) {
    ll ci;
    cin >> ci;
    if (c.back() != ci)
      c.push_back(ci);
  }
  map<ll, ll> m;
  m.insert({c[0], 1});
  vector<ll> dp(n, 0);
  dp[0] = 1;
  for (ll i = 1; i < n; ++i) {
    dp[i] = (dp[i - 1] + m[c[i]]) % mod;
    m[c[i]] = dp[i];
  }
  cout << dp.back() << endl;
}

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

const int mod = 1e9 + 7;

int main() {
  ll n;
  cin >> n;
  vector<ll> c(1);
  cin >> c[0];
  for (ll i = 1; i < n; ++i) {
    ll ci;
    cin >> ci;
    if (c.back() != ci)
      c.push_back(ci);
  }
  n = c.size();
  map<ll, ll> m;
  m.insert({c[0], 1});
  vector<ll> dp(n, 0);
  dp[0] = 1;
  for (ll i = 1; i < n; ++i) {
    dp[i] = (dp[i - 1] + m[c[i]]) % mod;
    m[c[i]] = dp[i];
  }
  cout << dp.back() << endl;
}
Problem p03096 (C++)

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

int main() {
  const int mod = 1000000007;
  int n, patterns = 1, prev = 0;
  cin >> n;
  vector<int> p(n + 1);
  for (int i = 0; i < n; i++) {
    int c, w;
    cin >> c;
    if (c == prev)
      continue;
    w = (patterns + p.at(c)) % mod;
    patterns = w;
    p.at(c) = w;
    prev = c;
  }
  cout << patterns << endl;
}


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

int main() {
  const int mod = 1000000007;
  int n, patterns = 1, prev = 0;
  cin >> n;
  vector<int> p(200001);
  for (int i = 0; i < n; i++) {
    int c, w;
    cin >> c;
    if (c == prev)
      continue;
    w = (patterns + p.at(c)) % mod;
    patterns = w;
    p.at(c) = w;
    prev = c;
  }
  cout << patterns << endl;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

long long MOD = 1000000007;

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

  vector<long long> pattern(N, 0);
  pattern[0] = 1;
  vector<int> prev(N + 1, -1);
  for (int i = 0; i < N; i++) {
    int c = C[i];
    if (prev[c] != -1 && prev[c] != i - 1) {
      pattern[i] = (pattern[prev[c]] + pattern[i - 1]) % MOD;
    } else if (i > 0) {
      pattern[i] = pattern[i - 1];
    }
    prev[c] = i;
  }
  cout << pattern[N - 1] << endl;
}

Fixed submission:
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

long long MOD = 1000000007;

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

  vector<long long> pattern(N, 0);
  pattern[0] = 1;
  vector<int> prev(200000 + 1, -1);
  for (int i = 0; i < N; i++) {
    int c = C[i];
    if (prev[c] != -1 && prev[c] != i - 1) {
      pattern[i] = (pattern[prev[c]] + pattern[i - 1]) % MOD;
    } else if (i > 0) {
      pattern[i] = pattern[i - 1];
    }
    prev[c] = i;
  }
  cout << pattern[N - 1] << endl;
}
Problem p03096 (C++)

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

int main() {
  int N;
  cin >> N;
  vector<int> C(N);
  for (int i = 0; i < N; ++i)
    cin >> C[i];
  C.erase(unique(begin(C), end(C)), end(C));

  const long long MOD = 1000000007;
  vector<long long> sum(N + 1);
  sum[C[0]] = 1;
  for (int i = 1; i < C.size(); ++i) {
    sum[C[i]] = (sum[C[i]] + sum[C[i - 1]]) % MOD;
  }
  cout << sum[*rbegin(C)] << endl;
}

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

int main() {
  int N;
  cin >> N;
  vector<int> C(N);
  for (int i = 0; i < N; ++i)
    cin >> C[i];
  C.erase(unique(begin(C), end(C)), end(C));

  const long long MOD = 1000000007;
  long long sum[200001] = {};
  sum[C[0]] = 1;
  for (int i = 1; i < C.size(); ++i) {
    sum[C[i]] = (sum[C[i]] + sum[C[i - 1]]) % MOD;
  }
  cout << sum[*rbegin(C)] << endl;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
using namespace std;
using ll = long long;
const static int MOD = 1000000007;

int main() {
  int N;
  scanf("%d", &N);
  int C[N];
  scanf("%d", C);
  int id = 0;
  for (int i = 0; i < N - 1; ++i) {
    int tmp;
    scanf("%d", &tmp);
    if (tmp != C[id])
      C[++id] = tmp;
  }
  int last[N + 1];
  memset(last, -1, sizeof last);
  ll dp[N];
  dp[0] = 1;
  last[C[0]] = 0;
  for (int i = 0; i < id; ++i) {
    dp[i + 1] = dp[i];
    if (last[C[i + 1]] >= 0) {
      (dp[i + 1] += dp[last[C[i + 1]]]) %= MOD;
    }
    last[C[i + 1]] = i + 1;
  }
  printf("%lld\n", dp[id]);
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
using namespace std;
using ll = long long;
const static int MOD = 1000000007;

int main() {
  int N;
  scanf("%d", &N);
  int C[N];
  scanf("%d", C);
  int id = 0;
  for (int i = 0; i < N - 1; ++i) {
    int tmp;
    scanf("%d", &tmp);
    if (tmp != C[id])
      C[++id] = tmp;
  }
  int last[200001];
  memset(last, -1, sizeof last);
  ll dp[N];
  dp[0] = 1;
  last[C[0]] = 0;
  for (int i = 0; i < id; ++i) {
    dp[i + 1] = dp[i];
    if (last[C[i + 1]] >= 0) {
      (dp[i + 1] += dp[last[C[i + 1]]]) %= MOD;
    }
    last[C[i + 1]] = i + 1;
  }
  printf("%lld\n", dp[id]);
  return 0;
}
Problem p03096 (C++)

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

#define For(i, a, b) for (int(i) = (a); (i) < (b); i++)
#define Rep(i, n) For((i), 0, (n))
#define All(a) (a).begin(), (a).end()
#define sp " "
#define INF 1e18
#define INT_INF 1e9

typedef long long ll;
const ll MOD = 1000000007;
const ll MOD_9 = 998244353;

int N;

int main() {
  cin >> N;
  vector<int> C(N);
  vector<int> F(200010, 0);
  long long DP[N + 1];
  DP[0] = 1;
  Rep(i, N) cin >> C[i + 1];
  Rep(i, N) {
    if (F[C[i + 1]] == 0)
      DP[i + 1] = DP[i];
    else if (F[C[i + 1]] == i)
      DP[i + 1] = DP[i];
    else
      DP[i + 1] = DP[i] + DP[F[C[i + 1]]];
    F[C[i + 1]] = i + 1;
    DP[i + 1] %= MOD;
    // cout << DP[i+1] << endl;
  }
  cout << DP[N] << endl;
  // cin >> N;
}

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

#define For(i, a, b) for (int(i) = (a); (i) < (b); i++)
#define Rep(i, n) For((i), 0, (n))
#define All(a) (a).begin(), (a).end()
#define sp " "
#define INF 1e18
#define INT_INF 1e9

typedef long long ll;
const ll MOD = 1000000007;
const ll MOD_9 = 998244353;

int N;

int main() {
  cin >> N;
  vector<int> C(N + 1);
  vector<int> F(200010, 0);
  long long DP[N + 1];
  DP[0] = 1;
  Rep(i, N) cin >> C[i + 1];
  Rep(i, N) {
    if (F[C[i + 1]] == 0)
      DP[i + 1] = DP[i];
    else if (F[C[i + 1]] == i)
      DP[i + 1] = DP[i];
    else
      DP[i + 1] = DP[i] + DP[F[C[i + 1]]];
    F[C[i + 1]] = i + 1;
    DP[i + 1] %= MOD;
    // cout << DP[i+1] << endl;
  }
  cout << DP[N] << endl;
  // cin >> N;
}
Problem p03096 (C++)

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

#define all(s) s.begin(), s.end()
#define vi vector<int>
#define pb push_back
#define ii pair<int, int>
#define x first
#define y second

const int N = 200005;
const int MOD = 1000000007;

int dp[N], c[N];
vi v[N];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  dp[0] = 1;
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
    v[c[i]].pb(i);
    for (int j = 0; j < (int)v[c[i]].size(); j++) {
      if (j > 0 && v[c[i]][j] == v[c[i]][j - 1] + 1)
        continue;
      int u = v[c[i]][j];
      dp[i] = (dp[i] + dp[u - 1]) % MOD;
    }
  }
  cout << dp[n];
  return 0;
}

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

#define all(s) s.begin(), s.end()
#define vi vector<int>
#define pb push_back
#define ii pair<int, int>
#define x first
#define y second

const int N = 200005;
const int MOD = 1000000007;

int dp[N], c[N];
vi v[N];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  dp[0] = 1;
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
    v[c[i]].pb(i);
    if (v[c[i]].size() == 1) {
      dp[i] = dp[i - 1];
      continue;
    }
    int u = v[c[i]][v[c[i]].size() - 2];
    if (u == i - 1) {
      dp[i] = dp[u];
    } else {
      dp[i] = (dp[i - 1] + dp[u]) % MOD;
    }
  }
  cout << dp[n];
  return 0;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>

using namespace std;
using ll = long long;
using ld = double;
using P = pair<ll, ll>;
using T = tuple<ll, ll, ll, ll>; //(x, y, dx, dy)
const ll INF = 1e17;
const ld eps = 1e-10;
const ll MOD = 1e9 + 7;

const double pi = acos(-1.0);

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N;
  cin >> N;
  vector<ll> c(N);
  for (int i = 0; i < N; i++) {
    cin >> c[i];
    c[i]--;
  }
  vector<ll> sum(N);
  vector<ll> dp(N + 1);
  dp[0] = 1;
  for (int i = 0; i < N; i++) {
    dp[i + 1] = (dp[i + 1] + dp[i]) % MOD;
    if (i == 0 || c[i] != c[i - 1]) {
      dp[i + 1] = (dp[i + 1] + sum[c[i]]) % MOD;
      sum[c[i]] = (sum[c[i]] + dp[i]) % MOD;
    }
  }
  cout << dp[N] << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>

using namespace std;
using ll = long long;
using ld = double;
using P = pair<ll, ll>;
using T = tuple<ll, ll, ll, ll>; //(x, y, dx, dy)
const ll INF = 1e17;
const ld eps = 1e-10;
const ll MOD = 1e9 + 7;

const double pi = acos(-1.0);

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N;
  cin >> N;
  vector<ll> c(N);
  for (int i = 0; i < N; i++) {
    cin >> c[i];
    c[i]--;
  }
  vector<ll> sum(2e5 + 10);
  vector<ll> dp(N + 1);
  dp[0] = 1;
  for (int i = 0; i < N; i++) {
    dp[i + 1] = (dp[i + 1] + dp[i]) % MOD;
    if (i == 0 || c[i] != c[i - 1]) {
      dp[i + 1] = (dp[i + 1] + sum[c[i]]) % MOD;
      sum[c[i]] = (sum[c[i]] + dp[i]) % MOD;
    }
  }
  cout << dp[N] << endl;
  return 0;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define P 1000000007
using namespace std;
int f[120000], sum[120000];
int a[120000];
int n;
int main() {
  scanf("%d", &n);
  f[0] = 1;
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) {
    f[i] = f[i - 1];
    if (a[i] != a[i - 1])
      (f[i] += sum[a[i]]) %= P;
    if (a[i] != a[i - 1])
      (sum[a[i]] = f[i]) %= P;
  }
  printf("%d\n", f[n]);
}

Fixed submission:
#include <bits/stdc++.h>
#define P 1000000007
using namespace std;
int f[220000], sum[220000];
int a[220000];
int n;
int main() {
  scanf("%d", &n);
  f[0] = 1;
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) {
    f[i] = f[i - 1];
    if (a[i] != a[i - 1])
      (f[i] += sum[a[i]]) %= P;
    if (a[i] != a[i - 1])
      (sum[a[i]] = f[i]) %= P;
  }
  printf("%d\n", f[n]);
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <array>
#include <bits/stdc++.h>
using namespace std;
using ULL = unsigned long long;
using UL = unsigned;
using LL = long long;
#define rep(i, n) for (UL i = 0; i < (n); i++)

struct Problem {

  void Solve() {
    UL N;
    cin >> N;
    vector<UL> C(N);
    rep(i, N) {
      cin >> C[i];
      C[i]--;
    }
    vector<ULL> dp(N + 1);
    dp[0] = 1;
    vector<UL> P(N);
    vector<UL> CP;
    CP.assign(N, (UL)-1);
    rep(i, N) {
      P[i] = CP[C[i]];
      CP[C[i]] = i;
    }
    // rep(i, N) cout << P[i] << " "; cout << endl;
    rep(i, N) {
      dp[i + 1] = dp[i];
      if (P[i] != -1)
        if (P[i] != i - 1)
          dp[i + 1] += dp[P[i] + 1];
      dp[i + 1] %= 1000000007;
    }
    // rep(i, N) cout << dp[i] << " ";
    cout << dp[N] << endl;
  }

  Problem();
};
int main() {
  unique_ptr<Problem> p(new Problem());
  p->Solve();
  return 0;
}
Problem::Problem() { cout << fixed << setprecision(10); }

Fixed submission:
#include <array>
#include <bits/stdc++.h>
using namespace std;
using ULL = unsigned long long;
using UL = unsigned;
using LL = long long;
#define rep(i, n) for (UL i = 0; i < (n); i++)

struct Problem {

  void Solve() {
    UL N;
    cin >> N;
    vector<UL> C(N);
    rep(i, N) {
      cin >> C[i];
      C[i]--;
    }
    vector<ULL> dp(N + 1);
    dp[0] = 1;
    vector<UL> P(N);
    vector<UL> CP;
    CP.assign(200000, (UL)-1);
    rep(i, N) {
      P[i] = CP[C[i]];
      CP[C[i]] = i;
    }
    // rep(i, N) cout << P[i] << " "; cout << endl;
    rep(i, N) {
      dp[i + 1] = dp[i];
      if (P[i] != -1)
        if (P[i] != i - 1)
          dp[i + 1] += dp[P[i] + 1];
      dp[i + 1] %= 1000000007;
    }
    // rep(i, N) cout << dp[i] << " ";
    cout << dp[N] << endl;
  }

  Problem();
};
int main() {
  unique_ptr<Problem> p(new Problem());
  p->Solve();
  return 0;
}
Problem::Problem() { cout << fixed << setprecision(10); }
Problem p03096 (C++)

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

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

  ll MOD = 1e9 + 7;
  int N;
  cin >> N;
  vector<int> C(N, 0);
  for (int i = 0; i < N; i++) {
    int c;
    cin >> c;
    c--;
    C[i] = c;
  }

  vector<ll> ans(N, 0);
  vector<ll> pos(N, -1);

  for (int i = 0; i < N; i++) {
    int c = C[i];
    if (pos[c] != -1 && C[i] != C[i - 1]) {
      int p = pos[c];
      ans[i] = (ans[i - 1] + ans[p] + 1) % MOD;
    } else if (i > 0) {
      ans[i] = ans[i - 1];
    }
    pos[c] = i;
  }
  cout << (ans[N - 1] + 1) % MOD << '\n';

  return 0;
}

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

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

  ll MOD = 1e9 + 7;
  int N;
  cin >> N;
  vector<int> C(N, 0);
  for (int i = 0; i < N; i++) {
    int c;
    cin >> c;
    c--;
    C[i] = c;
  }

  vector<ll> ans(N, 0);
  vector<ll> pos(2e5 + 10, -1);

  for (int i = 0; i < N; i++) {
    int c = C[i];
    if (pos[c] != -1 && C[i] != C[i - 1]) {
      int p = pos[c];
      ans[i] = (ans[i - 1] + ans[p] + 1) % MOD;
    } else if (i > 0) {
      ans[i] = ans[i - 1];
    }
    pos[c] = i;
  }
  cout << (ans[N - 1] + 1) % MOD << '\n';

  return 0;
}
Problem p03096 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(), a.end()
#define sz(x) (int)(x).size()
#define uni(x) x.erase(unique(rng(x)), x.end())
#define show(x) cout << #x << " = " << x << endl;
#define PQ(T) priority_queue<T, v(T), greater<T>>
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef set<int> S;
typedef queue<int> Q;
typedef queue<P> QP;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
const int MOD = 1000000007;

int main() {
  int N;
  cin >> N;
  vi A(N);
  rep(i, N) { cin >> A[i]; }
  vvi cnt(200010);
  rep(i, N) { cnt[A[i]].push_back(i); }
  vi dp(N + 1, 0);
  dp[0] = 1;
  rep(i, N) {
    vi c = cnt[A[i]];
    int p = lower_bound(rng(c), i) - c.begin();
    if (p + 1 < sz(c)) {
      int r = c[p + 1];
      if (r != i + 1) {
        dp[r] += dp[i];
        dp[r] %= MOD;
      }
    }
    dp[i + 1] += dp[i];
    dp[i + 1] %= MOD;
  }
  cout << dp[N] << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(), a.end()
#define sz(x) (int)(x).size()
#define uni(x) x.erase(unique(rng(x)), x.end())
#define show(x) cout << #x << " = " << x << endl;
#define PQ(T) priority_queue<T, v(T), greater<T>>
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef set<int> S;
typedef queue<int> Q;
typedef queue<P> QP;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
const int MOD = 1000000007;

int main() {
  int N;
  cin >> N;
  vi A(N);
  rep(i, N) { cin >> A[i]; }
  vvi cnt(200010);
  rep(i, N) { cnt[A[i]].push_back(i); }
  vi dp(N + 1, 0);
  dp[0] = 1;
  rep(i, N) {
    int p = lower_bound(rng(cnt[A[i]]), i) - cnt[A[i]].begin();
    if (p + 1 < sz(cnt[A[i]])) {
      int r = cnt[A[i]][p + 1];
      if (r != i + 1) {
        dp[r] += dp[i];
        dp[r] %= MOD;
      }
    }
    dp[i + 1] += dp[i];
    dp[i + 1] %= MOD;
  }
  cout << dp[N] << endl;

  return 0;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL_DEBUG
#include "LOCAL_DEBUG.hpp"
#endif
#define int long long
const int MOD = 1e9 + 7;

signed main() {

  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  vector<int> dp(n + 1, 0), pos(n + 1, -1);
  for (int i = 0; i < n; i++) {
    if (i > 0 && a[i - 1] == a[i]) {
      dp[i + 1] = dp[i];
      continue;
    }
    if (pos[a[i]] == -1) {
      dp[i + 1] = max(1LL, dp[i]);
      pos[a[i]] = i + 1;
    } else {
      dp[i + 1] = (dp[i] + dp[pos[a[i]]]) % MOD;
      pos[a[i]] = i + 1;
    }
  }
  cout << dp[n] << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL_DEBUG
#include "LOCAL_DEBUG.hpp"
#endif
#define int long long
const int MOD = 1e9 + 7;

signed main() {

  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  vector<int> dp(n + 1, 0), pos(n + 1 + 2e5, -1);
  for (int i = 0; i < n; i++) {
    if (i > 0 && a[i - 1] == a[i]) {
      dp[i + 1] = dp[i];
      continue;
    }
    if (pos[a[i]] == -1) {
      dp[i + 1] = max(1LL, dp[i]);
      pos[a[i]] = i + 1;
    } else {
      dp[i + 1] = (dp[i] + dp[pos[a[i]]]) % MOD;
      pos[a[i]] = i + 1;
    }
  }
  cout << dp[n] << endl;

  return 0;
}
Problem p03096 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"

using namespace std;

const long mod = 1e9 + 7;

void solve() {
  int N;
  cin >> N;
  vector<int> as(N), nx[200020];
  for (int i = 0; i < N; i++) {
    cin >> as[i];
    nx[as[i]].push_back(i);
  }
  for (int i = 0; i < 200020; i++) {
    nx[i].push_back(N + 1);
  }
  vector<long> dp(N + 1);
  dp[0] = 1;
  for (int i = 0; i < N; i++) {
    int a = as[i];
    auto v = nx[a];
    int j = *lower_bound(v.begin(), v.end(), i + 1);
    if (j != N + 1 && j != i + 1) {
      dp[j] += dp[i];
      dp[j] %= mod;
    }
    dp[i + 1] += dp[i];
    dp[i + 1] %= mod;
    // cout <<i << ' ' << j << ' ' << dp[i] << endl;
  }
  cout << dp[N] << endl;
}

int main(void) {
  solve();
  // cout << "yui(*-v・)yui" << endl;
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"

using namespace std;

const long mod = 1e9 + 7;

void solve() {
  int N;
  cin >> N;
  vector<int> as(N), nx[200020];
  for (int i = 0; i < N; i++) {
    cin >> as[i];
    nx[as[i]].push_back(i);
  }
  for (int i = 0; i < 200020; i++) {
    nx[i].push_back(N + 1);
  }
  vector<long> dp(N + 1);
  dp[0] = 1;
  for (int i = 0; i < N; i++) {
    int a = as[i];
    int j = *lower_bound(nx[a].begin(), nx[a].end(), i + 1);
    if (j != N + 1 && j != i + 1) {
      dp[j] += dp[i];
      dp[j] %= mod;
    }
    dp[i + 1] += dp[i];
    dp[i + 1] %= mod;
    // cout <<i << ' ' << j << ' ' << dp[i] << endl;
  }
  cout << dp[N] << endl;
}

int main(void) {
  solve();
  // cout << "yui(*-v・)yui" << endl;
  return 0;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define debug(x) std::cerr << #x << " = " << (x) << std::endl
using namespace std;
typedef long long LL;
const int MAXN = 1e5 + 17;
const int MOD = 1e9 + 7;
LL f[MAXN], lst[MAXN], pre[MAXN], a[MAXN];
int main(int argc, char const *argv[]) {
#ifdef noob
  freopen("Input.txt", "r", stdin);
  freopen("Output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    pre[i] = lst[a[i]];
    lst[a[i]] = i;
  }
  f[0] = 1;
  for (int i = 1; i <= n; ++i) {
    f[i] = f[i - 1];
    if (a[i - 1] != a[i] && pre[i] != 0)
      f[i] = (f[i] + f[pre[i]]) % MOD;
  }
  cout << (f[n] % MOD) << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define debug(x) std::cerr << #x << " = " << (x) << std::endl
using namespace std;
typedef long long LL;
const int MAXN = 2e5 + 17;
const int MOD = 1e9 + 7;
LL f[MAXN], lst[MAXN], pre[MAXN], a[MAXN];
int main(int argc, char const *argv[]) {
#ifdef noob
  freopen("Input.txt", "r", stdin);
  freopen("Output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    pre[i] = lst[a[i]];
    lst[a[i]] = i;
  }
  f[0] = 1;
  for (int i = 1; i <= n; ++i) {
    f[i] = f[i - 1];
    if (a[i - 1] != a[i] && pre[i] != 0)
      f[i] = (f[i] + f[pre[i]]) % MOD;
  }
  cout << (f[n] % MOD) << endl;
  return 0;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#define _USE_MATH_DEFINES
#include <functional>
#include <math.h>
using namespace std;

#define rep(i, x) for (ll i = 0; i < x; i++)
#define repn(i, x) for (ll i = 1; i <= x; i++)

typedef long long ll;
const ll INF = 1e17;
const ll MOD = 1000000007;
const ll MAX = 1000001;

ll max(ll a, ll b) {
  if (a > b) {
    return a;
  }
  return b;
}

ll min(ll a, ll b) {
  if (a > b) {
    return b;
  }
  return a;
}

ll gcd(ll a, ll b) {
  if (b == 0) {
    return a;
  }
  if (a < b) {
    return gcd(b, a);
  }
  return gcd(b, a % b);
}

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

struct edge {
  ll ind;
  ll fr;
  ll to;
  ll d;
};

///////////////////////////

int main() {

  ll N;
  cin >> N;
  vector<ll> C(N + 1);
  repn(i, N) cin >> C[i];

  vector<ll> pre(N + 1, -1), dp(N + 1, 0);

  dp[0] = 1;

  repn(i, N) {
    dp[i] = dp[i - 1];

    if (pre[C[i]] != -1 && pre[C[i]] != i - 1) {
      dp[i] += dp[pre[C[i]]];
    }

    pre[C[i]] = i;

    dp[i] %= MOD;

    // cout << dp[i] << endl;
  }

  cout << dp[N];

  system("PAUSE");
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#define _USE_MATH_DEFINES
#include <functional>
#include <math.h>
using namespace std;

#define rep(i, x) for (ll i = 0; i < x; i++)
#define repn(i, x) for (ll i = 1; i <= x; i++)

typedef long long ll;
const ll INF = 1e17;
const ll MOD = 1000000007;
const ll MAX = 1000001;

ll max(ll a, ll b) {
  if (a > b) {
    return a;
  }
  return b;
}

ll min(ll a, ll b) {
  if (a > b) {
    return b;
  }
  return a;
}

ll gcd(ll a, ll b) {
  if (b == 0) {
    return a;
  }
  if (a < b) {
    return gcd(b, a);
  }
  return gcd(b, a % b);
}

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

struct edge {
  ll ind;
  ll fr;
  ll to;
  ll d;
};

///////////////////////////

int main() {

  ll N;
  cin >> N;
  vector<ll> C(N + 1);
  repn(i, N) cin >> C[i];

  vector<ll> pre(300000, -1), dp(N + 1, 0);

  dp[0] = 1;

  repn(i, N) {
    dp[i] = dp[i - 1];

    if (pre[C[i]] != -1 && pre[C[i]] != i - 1) {
      dp[i] += dp[pre[C[i]]];
    }

    pre[C[i]] = i;

    dp[i] %= MOD;

    // cout << dp[i] << endl;
  }

  cout << dp[N];

  system("PAUSE");
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(a) a.begin(), a.end()

const int MOD = 1e9 + 7;

struct mint {
  int n;
  mint(int n_ = 0) : n(n_) {}
};

mint operator+(mint a, mint b) {
  a.n += b.n;
  if (a.n >= MOD)
    a.n -= MOD;
  return a;
}
mint operator-(mint a, mint b) {
  a.n -= b.n;
  if (a.n < 0)
    a.n += MOD;
  return a;
}
mint operator*(mint a, mint b) { return (long long)a.n * b.n % MOD; }
mint &operator+=(mint &a, mint b) { return a = a + b; }
mint &operator-=(mint &a, mint b) { return a = a - b; }
mint &operator*=(mint &a, mint b) { return a = a * b; }

int main() {
  int N;
  cin >> N;
  vector<int> C(N);
  vector<set<int>> st(N);
  REP(i, N) {
    cin >> C[i];
    st[C[i]].insert(i);
  }
  vector<mint> dp(N + 1);
  dp[0] = 1;
  for (int i = 0; i < N; i++) {
    auto it = st[C[i]].upper_bound(i);
    if (it != st[C[i]].end()) {
      int j = *it;
      if (j > i + 1) {
        dp[j] += dp[i];
      }
    }
    dp[i + 1] += dp[i];
  }
  cout << dp[N].n << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(a) a.begin(), a.end()

const int MOD = 1e9 + 7;

struct mint {
  int n;
  mint(int n_ = 0) : n(n_) {}
};

mint operator+(mint a, mint b) {
  a.n += b.n;
  if (a.n >= MOD)
    a.n -= MOD;
  return a;
}
mint operator-(mint a, mint b) {
  a.n -= b.n;
  if (a.n < 0)
    a.n += MOD;
  return a;
}
mint operator*(mint a, mint b) { return (long long)a.n * b.n % MOD; }
mint &operator+=(mint &a, mint b) { return a = a + b; }
mint &operator-=(mint &a, mint b) { return a = a - b; }
mint &operator*=(mint &a, mint b) { return a = a * b; }

int main() {
  int N;
  cin >> N;
  vector<int> C(N);
  vector<set<int>> st(200001);
  REP(i, N) {
    cin >> C[i];
    st[C[i]].insert(i);
  }
  vector<mint> dp(N + 1);
  dp[0] = 1;
  for (int i = 0; i < N; i++) {
    auto it = st[C[i]].upper_bound(i);
    if (it != st[C[i]].end()) {
      int j = *it;
      if (j > i + 1) {
        dp[j] += dp[i];
      }
    }
    dp[i + 1] += dp[i];
  }
  cout << dp[N].n << endl;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define ll int64_t
#define Rep(i, n) for (ll i = 0; i < n; i++)
using namespace std;
typedef vector<ll> vec;
typedef vector<vec> mat;

const int MOD = 1000000007;

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

  ll N;
  cin >> N;
  vector<ll> C(N);
  Rep(i, N) { cin >> C[i]; }

  vec pre(N + 1, -1);
  vec check(N, -1);
  Rep(i, N) {
    check[i] = pre[C[i]];
    pre[C[i]] = i;
  }

  // Rep (i, N) {
  //     cout << check[i] << " ";
  // }
  // cout << "\n";

  vec dp(N + 1);
  dp[0] = 1;
  Rep(i, N) {
    dp[i + 1] = dp[i];
    ll j = check[i];
    if (j == i - 1) {
      continue;
    }
    if (j != -1) {
      dp[i + 1] += dp[j + 1];
      dp[i + 1] %= MOD;
    }
  }

  cout << dp[N] << "\n";
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define ll int64_t
#define Rep(i, n) for (ll i = 0; i < n; i++)
using namespace std;
typedef vector<ll> vec;
typedef vector<vec> mat;

const int MOD = 1000000007;

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

  ll N;
  cin >> N;
  vector<ll> C(N);
  Rep(i, N) { cin >> C[i]; }

  vec pre(2e5 + 1, -1);
  vec check(N, -1);
  Rep(i, N) {
    check[i] = pre[C[i]];
    pre[C[i]] = i;
  }

  // Rep (i, N) {
  //     cout << check[i] << " ";
  // }
  // cout << "\n";

  vec dp(N + 1);
  dp[0] = 1;
  Rep(i, N) {
    dp[i + 1] = dp[i];
    ll j = check[i];
    if (j == i - 1) {
      continue;
    }
    if (j != -1) {
      dp[i + 1] += dp[j + 1];
      dp[i + 1] %= MOD;
    }
  }

  cout << dp[N] << "\n";
}
Problem p03096 (C++)

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

using namespace std;

typedef long long ll;

const int N = 100050;
const ll MOD = 1e9 + 7;
int n;
int a[N], p[N];
ll f[N];

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
  f[0] = 0;
  f[1] = 1;
  memset(p, 0, sizeof(p));
  p[a[1]] = 1;
  for (int i = 2; i <= n; ++i) {
    f[i] = f[i - 1];
    if (a[i] != a[i - 1])
      f[i] = (f[i] + f[p[a[i]]]) % MOD;
    p[a[i]] = i;
  }
  printf("%lld\n", f[n]);
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 200050;
const ll MOD = 1e9 + 7;
int n;
int a[N], p[N];
ll f[N];

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
  f[0] = 0;
  f[1] = 1;
  memset(p, 0, sizeof(p));
  p[a[1]] = 1;
  for (int i = 2; i <= n; ++i) {
    f[i] = f[i - 1];
    if (a[i] != a[i - 1])
      f[i] = (f[i] + f[p[a[i]]]) % MOD;
    p[a[i]] = i;
  }
  printf("%lld\n", f[n]);
}
Problem p03096 (C++)

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

const long long MOD = 1e9 + 7;
const int MAXN = 1e5;

int N;
int C[MAXN + 10];

long long dp[MAXN + 10], ss[MAXN + 10];

int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%d", &C[i]);
  }
  dp[N] = 1;
  for (int i = N - 1; i >= 0; i--) {
    int c = C[i];
    if (c != C[i + 1]) {
      ss[c] = (ss[c] + dp[i + 1]) % MOD;
    }
    dp[i] = ss[c];
  }
  printf("%lld\n", dp[0]);
}

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

const long long MOD = 1e9 + 7;
const int MAXN = 2e5;

int N;
int C[MAXN + 10];

long long dp[MAXN + 10], ss[MAXN + 10];

int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%d", &C[i]);
  }
  dp[N] = 1;
  for (int i = N - 1; i >= 0; i--) {
    int c = C[i];
    if (c != C[i + 1]) {
      ss[c] = (ss[c] + dp[i + 1]) % MOD;
    }
    dp[i] = ss[c];
  }
  printf("%lld\n", dp[0]);
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define int long long
#define vel vector<int>
#define vvel vector<vel>
#define vvvel vector<vvel>
#define veb vector<bool>
long long rui(long long a, long long n, long long p) {
  if (n == 0) {
    return 1;
  }
  int x = rui(a, n / 2, p);
  x = (x * x) % p;
  if (n % 2 == 1) {
    x = (x * a) % p;
  }
  return x;
}
signed main() {
  int n;
  cin >> n;
  vel c(n);
  for (int i = 0; i < n; i++) {
    cin >> c[i];
  }
  int p = 1000;
  p *= p * p;
  p += 7;
  vel qc(1, c[0]);
  for (int i = 1; i < n; i++) {
    if (c[i] != c[i - 1]) {
      qc.push_back(c[i]);
    }
  }
  c = qc;
  n = c.size();
  vel dp(n + 1, 1);
  vel counter(20 * 1000 + 1, 0);
  for (int i = 1; i <= n; i++) {
    dp[i] = dp[i - 1];
    dp[i] += counter[c[i - 1]];
    dp[i] = dp[i] % p;
    counter[c[i - 1]] += dp[i - 1];
    counter[c[i - 1]] = counter[c[i - 1]] % p;
  }
  cout << dp[n] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define int long long
#define vel vector<int>
#define vvel vector<vel>
#define vvvel vector<vvel>
#define veb vector<bool>
long long rui(long long a, long long n, long long p) {
  if (n == 0) {
    return 1;
  }
  int x = rui(a, n / 2, p);
  x = (x * x) % p;
  if (n % 2 == 1) {
    x = (x * a) % p;
  }
  return x;
}
signed main() {
  int n;
  cin >> n;
  vel c(n);
  for (int i = 0; i < n; i++) {
    cin >> c[i];
  }
  int p = 1000;
  p *= p * p;
  p += 7;
  vel qc(1, c[0]);
  for (int i = 1; i < n; i++) {
    if (c[i] != c[i - 1]) {
      qc.push_back(c[i]);
    }
  }
  c = qc;
  n = c.size();
  vel dp(n + 1, 1);
  vel counter(20 * 10000 + 1, 0);
  for (int i = 1; i <= n; i++) {
    dp[i] = dp[i - 1];
    dp[i] += counter[c[i - 1]];
    dp[i] = dp[i] % p;
    counter[c[i - 1]] += dp[i - 1];
    counter[c[i - 1]] = counter[c[i - 1]] % p;
  }
  cout << dp[n] << endl;
  return 0;
}
Problem p03096 (C++)

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

int main() {
  ll mod = 1000000007;
  ll N;
  cin >> N;
  vector<ll> C(N);
  vector<vector<ll>> color(N);
  for (ll i = 0; i < N; ++i) {
    ll c;
    cin >> c;
    C.at(i) = c - 1;
    color.at(c - 1).push_back(i);
  }
  vector<ll> dp(N + 1, 0);
  dp.at(0) = 1;
  for (ll i = 0; i < N; ++i) {
    dp.at(i + 1) = dp.at(i);
    ll index =
        lower_bound(color.at(C.at(i)).begin(), color.at(C.at(i)).end(), i) -
        color.at(C.at(i)).begin() - 1;
    if (index >= 0) {
      ll j = color.at(C.at(i)).at(index);
      if (i - j > 1) {
        // cout << i << ' ' << j << endl;
        dp.at(i + 1) = (dp.at(i + 1) + dp.at(j + 1)) % mod;
      }
    }
  }
  cout << dp.at(N) << endl;
  // for (ll i = 0; i < N + 1; ++i) {
  //   cout << dp.at(i) << ' ';
  // }
  // cout << endl;
}


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

int main() {
  ll mod = 1000000007;
  ll N;
  cin >> N;
  vector<ll> C(N);
  vector<vector<ll>> color(2 * 100000);
  for (ll i = 0; i < N; ++i) {
    ll c;
    cin >> c;
    C.at(i) = c - 1;
    color.at(c - 1).push_back(i);
  }
  vector<ll> dp(N + 1, 0);
  dp.at(0) = 1;
  for (ll i = 0; i < N; ++i) {
    dp.at(i + 1) = dp.at(i);
    ll index =
        lower_bound(color.at(C.at(i)).begin(), color.at(C.at(i)).end(), i) -
        color.at(C.at(i)).begin() - 1;
    if (index >= 0) {
      ll j = color.at(C.at(i)).at(index);
      if (i - j > 1) {
        // cout << i << ' ' << j << endl;
        dp.at(i + 1) = (dp.at(i + 1) + dp.at(j + 1)) % mod;
      }
    }
  }
  cout << dp.at(N) << endl;
  // for (ll i = 0; i < N + 1; ++i) {
  //   cout << dp.at(i) << ' ';
  // }
  // cout << endl;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
#define ll long long
#define to_mod 1000000007
#define MAX_COLOR 200000

int main() {
  int n;
  cin >> n;
  vector<int> each_color[MAX_COLOR];
  int num_each_color[MAX_COLOR];
  int color_coll[n];
  int idx_each_color[n];
  ll comb_points[n];
  comb_points[0] = 1;
  for (int i = 0; i < MAX_COLOR; i++)
    num_each_color[i] = 0;
  for (int i = 0; i < n; i++) {
    int inp_color;
    cin >> inp_color;
    color_coll[i] = inp_color;
    idx_each_color[i] = num_each_color[inp_color];
    num_each_color[inp_color]++;
    each_color[inp_color].push_back(i);
  }
  for (int i = 1; i < n; i++) {
    int lst_color = color_coll[i];
    int idx_in_color = idx_each_color[i];
    if (idx_in_color == 0)
      comb_points[i] = comb_points[i - 1];
    else {
      int prev_idx_in_color = each_color[lst_color][idx_in_color - 1];
      int diff = each_color[lst_color][idx_in_color] - prev_idx_in_color;
      if (diff > 1)
        comb_points[i] = comb_points[prev_idx_in_color] + comb_points[i - 1];
      else
        comb_points[i] = comb_points[i - 1];
    }
    comb_points[i] = (comb_points[i] + to_mod) % to_mod;
  }

  cout << comb_points[n - 1] << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
#define ll long long
#define to_mod 1000000007
#define MAX_COLOR 200001

int main() {
  int n;
  cin >> n;
  vector<int> each_color[MAX_COLOR];
  int num_each_color[MAX_COLOR];
  int color_coll[n];
  int idx_each_color[n];
  ll comb_points[n];
  comb_points[0] = 1;
  for (int i = 0; i < MAX_COLOR; i++)
    num_each_color[i] = 0;
  for (int i = 0; i < n; i++) {
    int inp_color;
    cin >> inp_color;
    color_coll[i] = inp_color;
    idx_each_color[i] = num_each_color[inp_color];
    num_each_color[inp_color]++;
    each_color[inp_color].push_back(i);
  }
  for (int i = 1; i < n; i++) {
    int lst_color = color_coll[i];
    int idx_in_color = idx_each_color[i];
    if (idx_in_color == 0)
      comb_points[i] = comb_points[i - 1];
    else {
      int prev_idx_in_color = each_color[lst_color][idx_in_color - 1];
      int diff = each_color[lst_color][idx_in_color] - prev_idx_in_color;
      if (diff > 1)
        comb_points[i] = comb_points[prev_idx_in_color] + comb_points[i - 1];
      else
        comb_points[i] = comb_points[i - 1];
    }
    comb_points[i] = (comb_points[i] + to_mod) % to_mod;
  }

  cout << comb_points[n - 1] << endl;
  return 0;
}
Problem p03096 (C++)

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

const int N = 2e1 + 5, mod = 1e9 + 7;
int n, c[N], dp[N], last[N];

int main() {
#ifdef LOCAL
  freopen("./input.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  cin >> n;
  int cnt = 1, temp;
  for (int i = 1; i <= n; i++) {
    cin >> temp;
    if (temp != c[cnt - 1]) {
      c[cnt++] = temp;
    }
  }

  dp[0] = 1;
  for (int i = 1; i < cnt; i++) {
    dp[i] = dp[i - 1];
    if (last[c[i]] != 0)
      (dp[i] += dp[last[c[i]]]) %= mod;
    last[c[i]] = i;
  }
  cout << dp[cnt - 1] << endl;

  return 0;
}


Fixed submission:
// Created by sz
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int N = 2e5 + 5, mod = 1e9 + 7;
int n, c[N], dp[N], last[N];

int main() {
#ifdef LOCAL
  freopen("./input.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  cin >> n;
  int cnt = 1, temp;
  for (int i = 1; i <= n; i++) {
    cin >> temp;
    if (temp != c[cnt - 1]) {
      c[cnt++] = temp;
    }
  }

  dp[0] = 1;
  for (int i = 1; i < cnt; i++) {
    dp[i] = dp[i - 1];
    if (last[c[i]] != 0)
      (dp[i] += dp[last[c[i]]]) %= mod;
    last[c[i]] = i;
  }
  cout << dp[cnt - 1] << endl;

  return 0;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#define set0(x) memset(x, 0, sizeof(x))
#define FF first
#define SS second
#define PB push_back
#define MP make_pair
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
template <typename T> void read(T &x) {
  x = 0;
  char ch = getchar();
  int f = 1;
  while (!isdigit(ch)) {
    if (ch == '-')
      f *= -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - 48;
    ch = getchar();
  }
  x *= f;
}
int mod = 1000000007;
inline int sq(int x) { return 1ll * x * x % mod; }
inline int sub(int a, int b) { return a < b ? a - b + mod : a - b; }
inline int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
inline int mul(int a, int b) { return 1ll * a * b % mod; }
inline int pow(int a, int b) {
  if (b == 0)
    return 1;
  return b & 1 ? mul(sq(pow(a, b / 2)), a) : sq(pow(a, b / 2));
}
inline int inv(int a) { return pow(a, mod - 2); }
int n, a[100010], dp[100010], last[100010];
int main() {
  read(n);
  dp[0] = 1;
  for (int i = 1; i <= n; i++) {
    read(a[i]);
    dp[i] = add(dp[i], dp[i - 1]);
    if (last[a[i]] && last[a[i]] != i - 1)
      dp[i] = add(dp[i], dp[last[a[i]]]);
    last[a[i]] = i;
    // cout<<i<<' '<<dp[i]<<endl;
  }
  cout << dp[n] << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#define set0(x) memset(x, 0, sizeof(x))
#define FF first
#define SS second
#define PB push_back
#define MP make_pair
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
template <typename T> void read(T &x) {
  x = 0;
  char ch = getchar();
  int f = 1;
  while (!isdigit(ch)) {
    if (ch == '-')
      f *= -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - 48;
    ch = getchar();
  }
  x *= f;
}
int mod = 1000000007;
inline int sq(int x) { return 1ll * x * x % mod; }
inline int sub(int a, int b) { return a < b ? a - b + mod : a - b; }
inline int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
inline int mul(int a, int b) { return 1ll * a * b % mod; }
inline int pow(int a, int b) {
  if (b == 0)
    return 1;
  return b & 1 ? mul(sq(pow(a, b / 2)), a) : sq(pow(a, b / 2));
}
inline int inv(int a) { return pow(a, mod - 2); }
int n, a[200010], dp[200010], last[200010];
int main() {
  read(n);
  dp[0] = 1;
  for (int i = 1; i <= n; i++) {
    read(a[i]);
    dp[i] = add(dp[i], dp[i - 1]);
    if (last[a[i]] && last[a[i]] != i - 1)
      dp[i] = add(dp[i], dp[last[a[i]]]);
    last[a[i]] = i;
    // cout<<i<<' '<<dp[i]<<endl;
  }
  cout << dp[n] << endl;
  return 0;
}
Problem p03096 (C++)

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

#define int long long
#define rep(i, l, r) for (int i = (int)(l); i < (int)(r); i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)x.size())
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;

const int inf = 1LL << 60;
const int mod = 1e9 + 7;
const double eps = 1e-9;

/*{
  1 2 3
  1   2
  }*/

signed main() {
  int n;
  cin >> n;

  vi c(n);
  rep(i, 0, n) cin >> c[i];

  vi dp(n + 1);
  vi prev(n + 1, -1);
  dp[0] = 1;
  rep(i, 1, n + 1) {
    (dp[i] += dp[i - 1]) %= mod;

    int &p = prev[c[i - 1]];
    if (p != -1 and i - p > 1) {
      (dp[i] += dp[p]) %= mod;
    }

    p = i;
  }

  cout << dp[n] << endl;

  return 0;
}


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

#define int long long
#define rep(i, l, r) for (int i = (int)(l); i < (int)(r); i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)x.size())
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;

const int inf = 1LL << 60;
const int mod = 1e9 + 7;
const double eps = 1e-9;

/*{
  1 2 3
  1   2
  }*/

signed main() {
  int n;
  cin >> n;

  vi c(n);
  rep(i, 0, n) cin >> c[i];

  vi dp(n + 1);
  vi prev(200001, -1);
  dp[0] = 1;
  rep(i, 1, n + 1) {
    (dp[i] += dp[i - 1]) %= mod;

    int &p = prev[c[i - 1]];
    if (p != -1 and i - p > 1) {
      (dp[i] += dp[p]) %= mod;
    }

    p = i;
  }

  cout << dp[n] << endl;

  return 0;
}
Problem p03096 (C++)

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

int main() {
  int N;
  cin >> N;
  int C[N], Sum[20001] = {}, DP[N];
  for (int i = 0; i < N; i++) {
    cin >> C[i];
  }

  DP[0] = 1;
  Sum[C[0]] = 1;
  for (int i = 1; i < N; i++) {
    if (C[i] == C[i - 1]) {
      DP[i] = DP[i - 1];
    } else {
      DP[i] = Sum[C[i]] + DP[i - 1];
      DP[i] %= mod;
      Sum[C[i]] = DP[i];
    }
  }

  cout << DP[N - 1];
  return 0;
}

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

int main() {
  int N;
  cin >> N;
  int C[N], Sum[200001] = {}, DP[N];
  for (int i = 0; i < N; i++) {
    cin >> C[i];
  }

  DP[0] = 1;
  Sum[C[0]] = 1;
  for (int i = 1; i < N; i++) {
    if (C[i] == C[i - 1]) {
      DP[i] = DP[i - 1];
    } else {
      DP[i] = Sum[C[i]] + DP[i - 1];
      DP[i] %= mod;
      Sum[C[i]] = DP[i];
    }
  }

  cout << DP[N - 1];
  return 0;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
// B.

#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

typedef long long LL;
static const LL MOD = 1000000007LL;

int n;
vector<int> c;

LL solve() {
  vector<LL> memo = vector<LL>(n + 1, 1);
  vector<int> p(100001, -1);
  LL r = 1;
  int last = c[0];
  p[c[0]] = 0;
  memo[0] = r;
  for (int i = 1; i < n; ++i) {
    memo[i] = r;
    if (c[i] != last) {
      if (p[c[i]] >= 0) {
        r = (r + memo[p[c[i]]]) % MOD;
      }
      p[c[i]] = i;
    }
    memo[i] = r;
    last = c[i];
  }
  return r;
}

int main(int argc, char *argv[]) {
  cin >> n;
  c = vector<int>(n + 1, -1);
  for (int i = 0; i < n; ++i) {
    cin >> c[i];
  }
  LL ans = solve();
  cout << ans << endl;
  return 0;
}


Fixed submission:
// B.

#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

typedef long long LL;
static const LL MOD = 1000000007LL;

int n;
vector<int> c;

LL solve() {
  vector<LL> memo = vector<LL>(n + 1, 1);
  vector<int> p(200001, -1);
  LL r = 1;
  int last = c[0];
  p[c[0]] = 0;
  memo[0] = r;
  for (int i = 1; i < n; ++i) {
    memo[i] = r;
    if (c[i] != last) {
      if (p[c[i]] >= 0) {
        r = (r + memo[p[c[i]]]) % MOD;
      }
      p[c[i]] = i;
    }
    memo[i] = r;
    last = c[i];
  }
  return r;
}

int main(int argc, char *argv[]) {
  cin >> n;
  c = vector<int>(n + 1, -1);
  for (int i = 0; i < n; ++i) {
    cin >> c[i];
  }
  LL ans = solve();
  cout << ans << endl;
  return 0;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define ll long long
const double EPS = 1e-10;
const ll MOD = 7 + (1e+9);

int main() {
  ll n;
  cin >> n;
  vector<ll> c(n, 0);
  for (ll i = 0; i < n; i++) {
    cin >> c[i];
    c[i]--;
  }
  vector<ll> dp(n, 0);
  vector<ll> v(n, 0);
  vector<ll> vv(n, -1);
  dp[n - 1] = 1;
  v[c[n - 1]] = 1;
  vv[c[n - 1]] = n - 1;
  for (ll i = n - 2; i >= 0; i--) {
    if (v[c[i]] == 0 || vv[c[i]] - i == 1) {
      dp[i] = dp[i + 1];
      v[c[i]] = dp[i];
    } else {
      dp[i] = dp[i + 1] + v[c[i]];
      v[c[i]] = dp[i];
    }
    vv[c[i]] = i;
    dp[i] %= MOD;
    v[c[i]] %= MOD;
  }
  cout << dp[0] << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define ll long long
const double EPS = 1e-10;
const ll MOD = 7 + (1e+9);

int main() {
  ll n;
  cin >> n;
  if (n == 1 || n == 2) {
    cout << 1 << endl;
    return 0;
  }
  vector<ll> c(n, 0);
  for (ll i = 0; i < n; i++) {
    cin >> c[i];
    c[i]--;
  }
  vector<ll> dp(n, 0);
  vector<ll> v(n, 0);
  vector<ll> vv(n, -1);
  dp[n - 1] = 1;
  v[c[n - 1]] = 1;
  vv[c[n - 1]] = n - 1;
  for (ll i = n - 2; i >= 0; i--) {
    if (v[c[i]] == 0 || vv[c[i]] - i == 1) {
      dp[i] = dp[i + 1];
      v[c[i]] = dp[i];
    } else {
      dp[i] = dp[i + 1] + v[c[i]];
      v[c[i]] = dp[i];
    }
    vv[c[i]] = i;
    dp[i] %= MOD;
    v[c[i]] %= MOD;
  }
  cout << dp[0] << endl;
}