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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define MAX 200005
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define f first
#define s second

ll n, m, i, j, k, t, l, r, tmp, ans, cnt, curr, flag, sum, mn, mx, mxi, x, pre,
    strt, en;
ll brr[MAX], arr[MAX], lst[MAX], dp[MAX];
string s;
queue<pair<pair<ll, ll>, ll>> q, q2;
pair<pair<ll, ll>, ll> pp;
map<ll, multiset<ll>> mpp;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
#endif
  cin >> n;

  for (i = 1; i <= n; i++) {
    cin >> arr[i];
    brr[i] = lst[arr[i]];
    lst[arr[i]] = i;
  }
  dp[0] = 1;
  for (i = 1; i <= n; i++) {
    if (!brr[i] || brr[i] == i - 1)
      dp[i] = dp[i - 1];
    else
      dp[i] = (dp[i - 1] + dp[brr[i]]) % MOD;
  }
  cout << dp[n];

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define MAX 200005
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define f first
#define s second

ll n, m, i, j, k, t, l, r, tmp, ans, cnt, curr, flag, sum, mn, mx, mxi, x, pre,
    strt, en;
ll brr[MAX], arr[MAX], lst[MAX], dp[MAX];
string s;
queue<pair<pair<ll, ll>, ll>> q, q2;
pair<pair<ll, ll>, ll> pp;
map<ll, multiset<ll>> mpp;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;

  for (i = 1; i <= n; i++) {
    cin >> arr[i];
    brr[i] = lst[arr[i]];
    lst[arr[i]] = i;
  }
  dp[0] = 1;
  for (i = 1; i <= n; i++) {
    if (!brr[i] || brr[i] == i - 1)
      dp[i] = dp[i - 1];
    else
      dp[i] = (dp[i - 1] + dp[brr[i]]) % MOD;
  }
  cout << dp[n];

  return 0;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
/*input
7
1
3
1
2
3
3
2
*/

#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 3;
const int MOD = 1e9 + 7;

int add(int a, int b) {
  int r = a + b;
  if (r >= MOD)
    r -= MOD;
  return r;
}

int mul(int a, int b) { return int((long long)a * b % MOD); }

int sub(int a, int b) {
  int r = MOD + a - b;
  if (r >= MOD)
    r -= MOD;
  return r;
}

map<int, int> last;

int c[N], prv[N], dp[N];

int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n;
  cin >> n;

  for (int i = 1; i <= n; ++i) {
    int x;
    cin >> x;
    int l = last[x];
    prv[i] = l;
    last[x] = i;
    c[i] = x;
  }

  dp[0] = 1;
  for (int i = 1; i <= n; ++i) {
    if (!prv[i] || (prv[i] + 1 == i)) {
      dp[i] = dp[i - 1];
      continue;
    }

    int p = prv[i];
    // cout << i << p << endl;
    dp[i] = add(dp[i - 1], dp[p]);
  }

  cout << dp[n];

  return 0;
}

Fixed submission:
/*input
7
1
3
1
2
3
3
2
*/

#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 3;
const int MOD = 1e9 + 7;

int add(int a, int b) {
  int r = a + b;
  if (r >= MOD)
    r -= MOD;
  return r;
}

int mul(int a, int b) { return int((long long)a * b % MOD); }

int sub(int a, int b) {
  int r = MOD + a - b;
  if (r >= MOD)
    r -= MOD;
  return r;
}

map<int, int> last;

int c[N], prv[N], dp[N];

int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n;
  cin >> n;

  for (int i = 1; i <= n; ++i) {
    int x;
    cin >> x;
    int l = last[x];
    prv[i] = l;
    last[x] = i;
    c[i] = x;
  }

  dp[0] = 1;
  for (int i = 1; i <= n; ++i) {
    if (!prv[i] || (prv[i] + 1 == i)) {
      dp[i] = dp[i - 1];
      continue;
    }

    int p = prv[i];
    // cout << i << p << endl;
    dp[i] = add(dp[i - 1], dp[p]);
  }

  cout << dp[n];

  return 0;
}
Problem p03096 (C++)

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

// definition {{{ 1

// scaning {{{ 2
#define Scd(x) scanf("%d", &x)
#define Scd2(x, y) scanf("%d%d", &x, &y)
#define Scd3(x, y, z) scanf("%d%d%d", &x, &y, &z)

#define Scll(x) scanf("%llu", &x)
#define Scll2(x, y) scanf("%llu%llu", &x, &y)
#define Scll3(x, y, z) scanf("%llu%llu%llu", &x, &y, &z)

#define Scc(c) scanf("%c", &c);
#define Scs(s) scanf("%s", s);
#define Scstr(s) scanf("%s", &s);
// }}} 2

// constants {{{ 2
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
// }}} 2

// systems {{{ 2
#define Rep(x, y) for (int x = 0; x < y; x++)
#define Repe(x, y, z) for (int x = z; x < y; x++)
// }}} 2

// output {{{ 2
#define YesNo(a) (a) ? printf("Yes\n") : printf("No\n");
// }}} 2

// }}} 1

int main() {

  int N;
  Scd(N);
  vector<int> c(N);
  Rep(i, N) Scd(c[i]);

  vector<int> mass(N, 0);
  vector<int> trans(233333, 0);

  int tmp = 0;
  for (int i = N - 1; i >= 0; i--) {
    if (tmp == c[i]) {
      mass[tmp] = i;
      continue;
    }
    tmp = c[i];
    trans[i] = mass[c[i]];
    mass[c[i]] = i;
  }

  ll mod = 1000000007;
  vector<ll> dp(N + 1, 0);
  dp[0] = 1;
  Rep(i, N) {
    dp[i + 1] += dp[i];
    if (trans[i]) {
      dp[trans[i]] += dp[i];
      dp[trans[i]] %= mod;
    }
    dp[i + 1] %= mod;
  }
  printf("%llu\n", dp[N] % mod);

  return 0;
}


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

// definition {{{ 1

// scaning {{{ 2
#define Scd(x) scanf("%d", &x)
#define Scd2(x, y) scanf("%d%d", &x, &y)
#define Scd3(x, y, z) scanf("%d%d%d", &x, &y, &z)

#define Scll(x) scanf("%llu", &x)
#define Scll2(x, y) scanf("%llu%llu", &x, &y)
#define Scll3(x, y, z) scanf("%llu%llu%llu", &x, &y, &z)

#define Scc(c) scanf("%c", &c);
#define Scs(s) scanf("%s", s);
#define Scstr(s) scanf("%s", &s);
// }}} 2

// constants {{{ 2
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
// }}} 2

// systems {{{ 2
#define Rep(x, y) for (int x = 0; x < y; x++)
#define Repe(x, y, z) for (int x = z; x < y; x++)
// }}} 2

// output {{{ 2
#define YesNo(a) (a) ? printf("Yes\n") : printf("No\n");
// }}} 2

// }}} 1

int main() {

  int N;
  Scd(N);
  vector<int> c(N);
  Rep(i, N) Scd(c[i]);

  vector<int> mass(200002, 0);
  vector<int> trans(N, 0);

  int tmp = 0;
  for (int i = N - 1; i >= 0; i--) {
    if (tmp == c[i]) {
      mass[tmp] = i;
      continue;
    }
    tmp = c[i];
    trans[i] = mass[c[i]];
    mass[c[i]] = i;
  }

  ll mod = 1000000007;
  vector<ll> dp(N + 1, 0);
  dp[0] = 1;
  Rep(i, N) {
    dp[i + 1] += dp[i];
    if (trans[i]) {
      dp[trans[i]] += dp[i];
      dp[trans[i]] %= mod;
    }
    dp[i + 1] %= mod;
  }
  printf("%llu\n", dp[N] % mod);

  return 0;
}
Problem p03096 (C++)

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

void solve() {
  int N;
  cin >> N;
  vector<int> C(N + 1);
  for (int i = 1; i <= N; i++)
    cin >> C.at(i);
  vector<int> DP(N + 1), s(N + 1);
  DP.at(0) = 1;
  for (int i = 1; i <= N; i++) {
    if (C.at(i) != C.at(i - 1)) {
      s.at(C.at(i)) += DP.at(i - 1);
      s.at(C.at(i)) %= 1000000007;
      // cout << "s.at(" << C.at(i) << ")=" << s.at(C.at(i)) << endl;
    }
    DP.at(i) = s.at(C.at(i));
    // cout << "DP.at(" << i << ")=" << DP.at(i) << endl;
  }
  // cout << "DP.at(N)=" << DP.at(N) << endl;
  cout << DP.at(N) << endl;
  return;
}

int main() { solve(); }

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

void solve() {
  int N;
  cin >> N;
  vector<int> C(N + 1);
  for (int i = 1; i <= N; i++)
    cin >> C.at(i);
  vector<int> DP(N + 1), s(200001);
  DP.at(0) = 1;
  for (int i = 1; i <= N; i++) {
    if (C.at(i) != C.at(i - 1)) {
      s.at(C.at(i)) += DP.at(i - 1);
      s.at(C.at(i)) %= 1000000007;
      // cout << "s.at(" << C.at(i) << ")=" << s.at(C.at(i)) << endl;
    }
    DP.at(i) = s.at(C.at(i));
    // cout << "DP.at(" << i << ")=" << DP.at(i) << endl;
  }
  // cout << "DP.at(N)=" << DP.at(N) << endl;
  cout << DP.at(N) << endl;
  return;
}

int main() { solve(); }
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define debug(x) cout << #x << " " << x << endl
#define fast ios_base::sync_with_stdio(false);
#define iscan1(x) scanf("%d", &x);
#define lscan1(x) scanf("%lld", &x);
#define iscan2(x, y) scanf("d", &x, &y);
#define lscan2(x, y) scanf("lld", &x, &y);
#define csprint(x) printf("Case %d: ", x);
#define iprint(x) printf("%d", x);
#define lprint(x) printf("%lld", x);
#define pnl printf("\n");
#define pg printf(" ");
ll ara[100005], dp[100005], lastpos[100005];
ll mod = 1000000007;
int main() {
  memset(lastpos, -1, sizeof lastpos);
  int n;
  cin >> n;
  int idx = 1;
  for (int i = 1; i <= n; i++) {
    ll a;
    cin >> a;
    if (a == ara[idx])
      continue;
    ara[++idx] = a;
  }
  for (int i = 1; i <= idx; i++) {
    if (lastpos[ara[i]] != -1) {
      dp[i] = (dp[i - 1] % mod + dp[lastpos[ara[i]]] % mod + 1) % mod;
      lastpos[ara[i]] = i;
    } else {
      lastpos[ara[i]] = i;
      dp[i] = dp[i - 1];
    }
  }
  cout << (dp[idx] + 1) % mod << endl;
}


Fixed submission:
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define debug(x) cout << #x << " " << x << endl
#define fast ios_base::sync_with_stdio(false);
#define iscan1(x) scanf("%d", &x);
#define lscan1(x) scanf("%lld", &x);
#define iscan2(x, y) scanf("d", &x, &y);
#define lscan2(x, y) scanf("lld", &x, &y);
#define csprint(x) printf("Case %d: ", x);
#define iprint(x) printf("%d", x);
#define lprint(x) printf("%lld", x);
#define pnl printf("\n");
#define pg printf(" ");
ll ara[200005], dp[200005], lastpos[200005];
ll mod = 1000000007;
int main() {
  memset(lastpos, -1, sizeof lastpos);
  int n;
  cin >> n;
  int idx = 1;
  for (int i = 1; i <= n; i++) {
    ll a;
    cin >> a;
    if (a == ara[idx])
      continue;
    ara[++idx] = a;
  }
  for (int i = 1; i <= idx; i++) {
    if (lastpos[ara[i]] != -1) {
      dp[i] = (dp[i - 1] % mod + dp[lastpos[ara[i]]] % mod + 1) % mod;
      lastpos[ara[i]] = i;
    } else {
      lastpos[ara[i]] = i;
      dp[i] = dp[i - 1];
    }
  }
  cout << (dp[idx] + 1) % mod << endl;
}
Problem p03096 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
typedef long long ll;
#define pb push_back

using namespace std;
ll n, a[200006];
ll dp[200006];
ll c[200006];
const ll mod = 1e9 + 7;
int main() {
  cin >> n;
  for (ll i = 1; i <= n; i++)
    cin >> a[i];
  dp[0] = 1;
  for (ll i = 1; i <= n; i++) {
    ll j = i;
    while (j <= n && a[j] == a[i])
      j++;
    dp[j - 1] = dp[i - 1];
    dp[j - 1] += c[a[i]];
    dp[j - 1] %= mod;
    c[a[i]] += dp[i - 1];
    c[a[i]] %= mod;
  }
  cout << (dp[n] % mod + mod) % mod << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
#define pb push_back

using namespace std;
ll n, a[200006];
ll dp[200006];
ll c[200006];
const ll mod = 1e9 + 7;
int main() {
  cin >> n;
  for (ll i = 1; i <= n; i++)
    cin >> a[i];
  dp[0] = 1;
  for (ll i = 1; i <= n; i++) {
    ll j = i;
    while (j <= n && a[j] == a[i])
      j++;
    dp[j - 1] = dp[i - 1];
    dp[j - 1] += c[a[i]];
    dp[j - 1] %= mod;
    c[a[i]] += dp[i - 1];
    c[a[i]] %= mod;
    i = j - 1;
  }
  cout << (dp[n] % mod + mod) % mod << endl;
  return 0;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 10;
#define mod 1000000007
int C[maxn], lst[maxn];
ll dp[maxn], sum[maxn];
bool hsr[maxn];
struct seg {
  int l, r;
  seg() {}
  seg(int l, int r) : l(l), r(r) {}
  bool operator<(const seg &t) const { return l < t.l; }
};
vector<seg> all;
int main() {
  memset(lst, -1, sizeof(lst));
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &C[i]);
    if (lst[C[i]] != -1 && lst[C[i]] != i - 1) {
      all.push_back(seg(lst[C[i]], i));
    }
    lst[C[i]] = i;
  }
  sort(all.begin(), all.end());
  //	for(int i=0;i<all.size();i++)
  //		cout<<all[i].l<<" "<<all[i].r<<endl;
  // dp[i] last right pos
  memset(dp, 0, sizeof(dp));
  memset(sum, 0, sizeof(sum));
  int j = 0;
  for (int i = 0; i < n; i++) {
    if (i)
      sum[i] = sum[i - 1];
    else
      sum[i] = 1;
    sum[i] += dp[i];
    if (sum[i] >= mod)
      sum[i] -= mod;
    if (all[j].l == i) {
      dp[all[j].r] += sum[i];
      //		cout<<all[j].r<<" "<<dp[all[j].r]<<endl;
      if (dp[all[j].r] >= mod)
        dp[all[j].r] -= mod;
      j++;
    }
    //	cout<<dp[i]<<" "<<sum[i]<<endl;system("pause");
  }
  printf("%lld\n", (sum[n - 1]) % mod);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 10;
#define mod 1000000007
int C[maxn], lst[maxn];
ll dp[maxn], sum[maxn];
bool hsr[maxn];
struct seg {
  int l, r;
  seg() {}
  seg(int l, int r) : l(l), r(r) {}
  bool operator<(const seg &t) const { return l < t.l; }
};
vector<seg> all;
int main() {
  memset(lst, -1, sizeof(lst));
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &C[i]);
    if (lst[C[i]] != -1 && lst[C[i]] != i - 1) {
      all.push_back(seg(lst[C[i]], i));
    }
    lst[C[i]] = i;
  }
  sort(all.begin(), all.end());
  //	for(int i=0;i<all.size();i++)
  //		cout<<all[i].l<<" "<<all[i].r<<endl;
  // dp[i] last right pos
  memset(dp, 0, sizeof(dp));
  memset(sum, 0, sizeof(sum));
  int j = 0;
  for (int i = 0; i < n; i++) {
    if (i)
      sum[i] = sum[i - 1];
    else
      sum[i] = 1;
    sum[i] += dp[i];
    if (sum[i] >= mod)
      sum[i] -= mod;
    if (j < all.size() && all[j].l == i) {
      dp[all[j].r] += sum[i];
      //		cout<<all[j].r<<" "<<dp[all[j].r]<<endl;
      if (dp[all[j].r] >= mod)
        dp[all[j].r] -= mod;
      j++;
    }
    //	cout<<dp[i]<<" "<<sum[i]<<endl;system("pause");
  }
  printf("%lld\n", (sum[n - 1]) % mod);
  return 0;
}
Problem p03096 (C++)

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

using namespace std;
const long long MOD = 1e9 + 7;

int main() {
  long long n, c;
  vector<long long> v;
  cin >> n;
  long long prev = -1;
  for (int i = 0; i < n; i++) {
    cin >> c;
    if (c != prev) {
      v.push_back(c);
      prev = c;
    }
  }
  if (n == 1) {
    cout << 1 << endl;
    return 0;
  }
  long long dp[100009];
  map<long long, long long> mp;
  dp[0] = 1;
  mp[v[0]] = 1;
  for (int i = 1; i < v.size(); i++) {
    dp[i] = (mp[v[i]] + dp[i - 1]) % MOD;
    mp[v[i]] = (dp[i - 1] + mp[v[i]]) % MOD;
  }
  cout << dp[v.size() - 1] << endl;
}


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

using namespace std;
const long long MOD = 1e9 + 7;

int main() {
  long long n, c;
  vector<long long> v;
  cin >> n;
  long long prev = -1;
  for (int i = 0; i < n; i++) {
    cin >> c;
    if (c != prev) {
      v.push_back(c);
      prev = c;
    }
  }
  if (n == 1) {
    cout << 1 << endl;
    return 0;
  }
  long long dp[200009];
  map<long long, long long> mp;
  dp[0] = 1;
  mp[v[0]] = 1;
  for (int i = 1; i < v.size(); i++) {
    dp[i] = (mp[v[i]] + dp[i - 1]) % MOD;
    mp[v[i]] = (dp[i - 1] + mp[v[i]]) % MOD;
  }
  cout << dp[v.size() - 1] << endl;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
/*------------------------------------
........Bismillahir Rahmanir Rahim....
..........created by Abdul Aziz.......
------------------------------------*/
#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <unordered_map>
#include <vector>
#define mod 998244353
#define int long long
#define ld long double
#define pb push_back
#define vi vector<int>
#define co(x) cout << x << '\n'
#define dbg(x) cerr << #x << " = " << x << '\n'
#define bitcount(x) (int)__builtin_popcount(x)
#define sz(x) (int)x.size()
#define all(a) a.begin(), a.end()
#define ff first
#define ss second
#define pii pair<int, int>
#define lcm(a, b) (a * b) / __gcd(a, b)
using namespace std;

inline void solve() {
  int n, pre = -1;
  cin >> n;
  vector<int> dp(100005, 0), c(100005, -1);
  dp[0] = 1;
  for (int i = 1, x; i <= n; i++) {
    cin >> x;
    dp[i] = dp[i - 1];
    if (c[x] != -1 and pre != x)
      dp[i] = dp[c[x]] + dp[i - 1];
    c[x] = i;
    pre = x;
    dp[i] %= 1000000007;
  }
  cout << dp[n] << endl;
}

signed main() {
  int n = 1; // cin>>n;
  while (n--)
    solve();
  return 0;
}


Fixed submission:
/*------------------------------------
........Bismillahir Rahmanir Rahim....
..........created by Abdul Aziz.......
------------------------------------*/
#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <unordered_map>
#include <vector>
#define mod 998244353
#define int long long
#define ld long double
#define pb push_back
#define vi vector<int>
#define co(x) cout << x << '\n'
#define dbg(x) cerr << #x << " = " << x << '\n'
#define bitcount(x) (int)__builtin_popcount(x)
#define sz(x) (int)x.size()
#define all(a) a.begin(), a.end()
#define ff first
#define ss second
#define pii pair<int, int>
#define lcm(a, b) (a * b) / __gcd(a, b)
using namespace std;

inline void solve() {
  int n, pre = -1;
  cin >> n;
  vector<int> dp(200005, 0), c(200005, -1);
  dp[0] = 1;
  for (int i = 1, x; i <= n; i++) {
    cin >> x;
    dp[i] = dp[i - 1];
    if (c[x] != -1 and pre != x)
      dp[i] = dp[c[x]] + dp[i - 1];
    c[x] = i;
    pre = x;
    dp[i] %= 1000000007;
  }
  cout << dp[n] << endl;
}

signed main() {
  int n = 1; // cin>>n;
  while (n--)
    solve();
  return 0;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define rt register int
#define l putchar('\n')
#define ll long long
#define r read()
#define p 1000000007
using namespace std;
inline ll read() {
  ll x = 0;
  char zf = 1;
  char ch = getchar();
  while (ch != '-' && !isdigit(ch))
    ch = getchar();
  if (ch == '-')
    zf = -1, ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  return x * zf;
}
void write(ll y) {
  if (y < 0)
    putchar('-'), y = -y;
  if (y > 9)
    write(y / 10);
  putchar(y % 10 + 48);
}
void writeln(const ll y) {
  write(y);
  putchar('\n');
}
int i, j, k, m, n, x, y, z, cnt, sum;
int a[100010], f[100010], la[100100], s[100010];
int main() {
  n = r;
  for (rt i = 1; i <= n; i++) {
    a[i] = r;
    if (a[i] == a[i - 1])
      n--, i--;
  }
  f[0] = s[0] = 1;
  for (rt i = 1; i <= n; i++) {
    if (la[a[i]])
      f[i] = s[la[a[i]]];
    la[a[i]] = i;
    s[i] = (s[i - 1] + f[i]) % p;
  }
  cout << s[n];
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define rt register int
#define l putchar('\n')
#define ll long long
#define r read()
#define p 1000000007
using namespace std;
inline ll read() {
  ll x = 0;
  char zf = 1;
  char ch = getchar();
  while (ch != '-' && !isdigit(ch))
    ch = getchar();
  if (ch == '-')
    zf = -1, ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  return x * zf;
}
void write(ll y) {
  if (y < 0)
    putchar('-'), y = -y;
  if (y > 9)
    write(y / 10);
  putchar(y % 10 + 48);
}
void writeln(const ll y) {
  write(y);
  putchar('\n');
}
int i, j, k, m, n, x, y, z, cnt, sum;
int a[1000010], f[1000010], la[1001000], s[1000010];
int main() {
  n = r;
  for (rt i = 1; i <= n; i++) {
    a[i] = r;
    if (a[i] == a[i - 1])
      n--, i--;
  }
  f[0] = s[0] = 1;
  for (rt i = 1; i <= n; i++) {
    if (la[a[i]])
      f[i] = s[la[a[i]]];
    la[a[i]] = i;
    s[i] = (s[i - 1] + f[i]) % p;
  }
  cout << s[n];
  return 0;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#define INF 1000000009
#define LINF 1000000000000000009
#define double long double
#define all(a) a.begin(), a.end()
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> PP;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll n, ll m) { return (m ? gcd(m, n % m) : n); }
ll lcm(ll n, ll m) { return n / gcd(n, m) * m; }

ll mod = 1000000007;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<int> p(n);
  vector<ll> dp(n);
  dp[0] = 1;
  p[a[0]] = 1;
  for (int i = 1; i < n; i++) {
    dp[i] = dp[i - 1];
    if (a[i] != a[i - 1]) {
      dp[i] += p[a[i]];
      dp[i] %= mod;
      p[a[i]] = dp[i];
    }
  }
  cout << dp[n - 1] << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#define INF 1000000009
#define LINF 1000000000000000009
#define double long double
#define all(a) a.begin(), a.end()
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> PP;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll n, ll m) { return (m ? gcd(m, n % m) : n); }
ll lcm(ll n, ll m) { return n / gcd(n, m) * m; }

ll mod = 1000000007;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<int> p(300000);
  vector<ll> dp(n);
  dp[0] = 1;
  p[a[0]] = 1;
  for (int i = 1; i < n; i++) {
    dp[i] = dp[i - 1];
    if (a[i] != a[i - 1]) {
      dp[i] += p[a[i]];
      dp[i] %= mod;
      p[a[i]] = dp[i];
    }
  }
  cout << dp[n - 1] << endl;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
// i ll be the king
#include <bits/stdc++.h>
#define ll long long int
#define sync                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
#define input(arr, n)                                                          \
  for (ll i1 = 0; i1 < n; i1++)                                                \
  cin >> arr[i1]
#define rep(n) for (ll i2 = 0; i2 < n; i2++)
#define vmp(v, a, b) v.push_back(make_pair(a, b))
#define si(a) scanf("%lld", &a)
#define pi(a) printf("%lld", a)
#define aset(a, n, k)                                                          \
  for (ll i3 = 0; i3 < n; i3++)                                                \
    a[i3] = k;
#define mod 1000000007
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>
using namespace std;
int main() {
  sync;
  ll n;
  cin >> n;
  ll a[n];
  input(a, n);
  map<ll, ll> mp;
  ll ans = 0, val = 0;
  vector<ll> arr;
  for (ll i = 0; i < n - 1; i++)
    if (a[i] != a[i + 1])
      arr.push_back(a[i]);
  arr.push_back(a[n - 1]);
  for (ll i = 0; i < n; i++) {
    val = ans;
    if (mp[arr[i]])
      ans = (ans + mp[arr[i]]) % mod;
    mp[arr[i]] = (mp[arr[i]] + 1 + val) % mod;
  }
  cout << ans + 1;
}


Fixed submission:
// i ll be the king
#include <bits/stdc++.h>
#define ll long long int
#define sync                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
#define input(arr, n)                                                          \
  for (ll i1 = 0; i1 < n; i1++)                                                \
  cin >> arr[i1]
#define rep(n) for (ll i2 = 0; i2 < n; i2++)
#define vmp(v, a, b) v.push_back(make_pair(a, b))
#define si(a) scanf("%lld", &a)
#define pi(a) printf("%lld", a)
#define aset(a, n, k)                                                          \
  for (ll i3 = 0; i3 < n; i3++)                                                \
    a[i3] = k;
#define mod 1000000007
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>
using namespace std;
int main() {
  sync;
  ll n;
  cin >> n;
  ll a[n];
  input(a, n);
  map<ll, ll> mp;
  ll ans = 0, val = 0;
  vector<ll> arr;
  for (ll i = 0; i < n - 1; i++)
    if (a[i] != a[i + 1])
      arr.push_back(a[i]);
  arr.push_back(a[n - 1]);
  for (ll i = 0; i < arr.size(); i++) {
    val = ans;
    if (mp[arr[i]])
      ans = (ans + mp[arr[i]]) % mod;
    mp[arr[i]] = (mp[arr[i]] + 1 + val) % mod;
  }
  cout << ans + 1;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vb = vector<bool>;
using vc = vector<char>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvc = vector<vector<char>>;
using pii = pair<int, int>;
#define fix10 cout << fixed << setprecision(10);
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define MOD 1000000007

int main() {
  int n;
  cin >> n;
  vector<int> clr(n);
  rep(i, n) cin >> clr.at(i);
  vector<long> dp(n + 1, 0);
  vector<long> hs(n + 1, 0);
  dp.at(0) = 1;
  hs.at(clr.at(0)) = 1;
  for (int i = 1; i <= n; i++) {
    int c = clr.at(i - 1);
    if (i != n) {
      if (clr.at(i - 1) == clr.at(i))
        continue;
    }
    dp.at(i) = hs.at(c) % MOD;
    if (i == n)
      continue;
    hs.at(clr.at(i)) += dp.at(i);
    hs.at(clr.at(i)) %= MOD;
  }
  cout << dp.at(n) % MOD << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vb = vector<bool>;
using vc = vector<char>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvc = vector<vector<char>>;
using pii = pair<int, int>;
#define fix10 cout << fixed << setprecision(10);
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define MOD 1000000007

int main() {
  int n;
  cin >> n;
  vector<int> clr(n);
  rep(i, n) cin >> clr.at(i);
  vector<long> dp(n + 1, 0);
  vector<long> hs(200001, 0);
  dp.at(0) = 1;
  hs.at(clr.at(0)) = 1;
  for (int i = 1; i <= n; i++) {
    int c = clr.at(i - 1);
    if (i != n) {
      if (clr.at(i - 1) == clr.at(i))
        continue;
    }
    dp.at(i) = hs.at(c) % MOD;
    if (i == n)
      continue;
    hs.at(clr.at(i)) += dp.at(i);
    hs.at(clr.at(i)) %= MOD;
  }
  cout << dp.at(n) % MOD << endl;
}
Problem p03096 (C++)

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

typedef long long ll;
const int mod = 1e+9 + 7;

// dpテーブルは 1-indexed
ll dp[200005];

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

  int n;
  cin >> n;
  vector<int> c(n);
  vector<vector<int>> pos(n); // 色がどの位置にあるか (0-indexed)
  for (int i = 0; i < n; ++i) {
    cin >> c[i];
    --c[i];
    pos[c[i]].push_back(i);
  }

  dp[0] = 1;
  for (int i = 0; i < n; ++i) {
    // 何も操作しない
    dp[i + 1] = (dp[i + 1] + dp[i]) % mod;

    // 今見ている c[i] が何番目であるか
    int id =
        lower_bound(pos[c[i]].begin(), pos[c[i]].end(), i) - pos[c[i]].begin();

    if (id > 0) {
      // 同じ色が隣り合っていないか確認
      if (i - pos[c[i]][id - 1] <= 1)
        continue;
      // 反転操作
      dp[i + 1] = (dp[i + 1] + dp[pos[c[i]][id - 1] + 1]) % mod;
    }
  }
  cout << dp[n] << endl;
}

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

typedef long long ll;
const int mod = 1e+9 + 7;

// dpテーブルは 1-indexed
ll dp[200005];

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

  int n;
  cin >> n;
  vector<int> c(n);
  vector<vector<int>> pos(200005); // 色がどの位置にあるか (0-indexed)
  for (int i = 0; i < n; ++i) {
    cin >> c[i];
    --c[i];
    pos[c[i]].push_back(i);
  }

  dp[0] = 1;
  for (int i = 0; i < n; ++i) {
    // 何も操作しない
    dp[i + 1] = (dp[i + 1] + dp[i]) % mod;

    // 今見ている c[i] が何番目であるか
    int id =
        lower_bound(pos[c[i]].begin(), pos[c[i]].end(), i) - pos[c[i]].begin();

    if (id > 0) {
      // 同じ色が隣り合っていないか確認
      if (i - pos[c[i]][id - 1] <= 1)
        continue;
      // 反転操作
      dp[i + 1] = (dp[i + 1] + dp[pos[c[i]][id - 1] + 1]) % mod;
    }
  }
  cout << dp[n] << endl;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#define mod 1000000007
#define maxn 100005
int n, val[maxn], dp[maxn], la[maxn], pos[maxn];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &val[i]);
    la[i] = pos[val[i]], pos[val[i]] = i;
  }
  dp[0] = 1;
  for (int i = 1; i <= n; i++) {
    dp[i] = dp[i - 1];
    if (val[i] != val[i - 1] && la[i])
      dp[i] = (dp[i] + dp[la[i]]) % mod;
  }
  printf("%d\n", dp[n]);
}

Fixed submission:
#include <cstdio>
#define mod 1000000007
#define maxn 200005
int n, val[maxn], dp[maxn], la[maxn], pos[maxn];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &val[i]);
    la[i] = pos[val[i]], pos[val[i]] = i;
  }
  dp[0] = 1;
  for (int i = 1; i <= n; i++) {
    dp[i] = dp[i - 1];
    if (val[i] != val[i - 1] && la[i])
      dp[i] = (dp[i] + dp[la[i]]) % mod;
  }
  printf("%d\n", dp[n]);
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

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

const int INF = 0x3f3f3f3f, MOD = 1000000007;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
/*-----------------------------------------*/
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  int n;
  cin >> n;
  vector<long long> memo(n + 1, 0);
  vector<int> c;
  int mae = -1;
  REP(i, n) {
    int cc;
    cin >> cc;
    if (mae != cc) {
      c.emplace_back(cc);
      mae = cc;
    }
  }
  n = c.size();
  long long pre = 1;
  REP(i, n) {
    (memo[c[i]] += pre) %= MOD;
    pre = memo[c[i]];
  }
  cout << pre << '\n';
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

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

const int INF = 0x3f3f3f3f, MOD = 1000000007;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
/*-----------------------------------------*/
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  int n;
  cin >> n;
  vector<long long> memo(200001, 0);
  vector<int> c;
  int mae = -1;
  REP(i, n) {
    int cc;
    cin >> cc;
    if (mae != cc) {
      c.emplace_back(cc);
      mae = cc;
    }
  }
  n = c.size();
  long long pre = 1;
  REP(i, n) {
    (memo[c[i]] += pre) %= MOD;
    pre = memo[c[i]];
  }
  cout << pre << '\n';
  return 0;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
int main() {
  long Nc;
  cin >> Nc;
  vector<long> C;
  for (long i = 0; i < Nc; i++) {
    long c;
    cin >> c;
    c--;
    if (i == 0 || c != C[C.size() - 1])
      C.push_back(c);
  }
  long N = C.size();

  vector<long> DP(N, 1), L(Nc, 0);
  for (long i = 0; i < N; i++) {
    if (i != 0)
      DP[i] = (DP[i - 1] + L[C[i]]) % MOD;
    L[C[i]] = (L[C[i]] + (i > 0 ? DP[i - 1] : 1)) % MOD;
  }
  cout << DP[N - 1] % MOD;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
int main() {
  long Nc;
  cin >> Nc;
  vector<long> C;
  for (long i = 0; i < Nc; i++) {
    long c;
    cin >> c;
    c--;
    if (i == 0 || c != C[C.size() - 1])
      C.push_back(c);
  }
  long N = C.size();
  Nc = 200000;

  vector<long> DP(N, 1), L(Nc, 0);
  for (long i = 0; i < N; i++) {
    if (i != 0)
      DP[i] = (DP[i - 1] + L[C[i]]) % MOD;
    L[C[i]] = (L[C[i]] + (i > 0 ? DP[i - 1] : 1)) % MOD;
  }
  cout << DP[N - 1] % MOD;
}
Problem p03096 (C++)

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

const int mod = 1e9 + 7;

int add(int &x, int y) {
  x += y;
  if (x >= mod) {
    x -= mod;
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  a.erase(unique(a.begin(), a.end()), a.end());
  n = (int)a.size();
  vector<int> dp(n, 0);
  dp[a[0]] = 1;
  for (int i = 1; i < n; i++) {
    add(dp[a[i]], dp[a[i - 1]]);
  }
  cout << dp[a[n - 1]] << '\n';
  return 0;
}

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

const int mod = 1e9 + 7;

int add(int &x, int y) {
  x += y;
  if (x >= mod) {
    x -= mod;
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  a.erase(unique(a.begin(), a.end()), a.end());
  n = (int)a.size();
  vector<int> dp((int)2e5, 0);
  dp[a[0]] = 1;
  for (int i = 1; i < n; i++) {
    add(dp[a[i]], dp[a[i - 1]]);
  }
  cout << dp[a[n - 1]] << '\n';
  return 0;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define Set(a, b) memset(a, b, sizeof(a))
using namespace std;
const int N = 1e5 + 10;
const int mod = 1e9 + 7;
template <typename T> inline void init(T &x) {
  x = 0;
  char ch = getchar();
  bool t = 0;
  for (; ch > '9' || ch < '0'; ch = getchar())
    if (ch == '-')
      t = 1;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = (x << 1) + (x << 3) + (ch - 48);
  if (t)
    x = -x;
  return;
}
typedef long long ll;
typedef unsigned long long ull;
template <typename T> inline void Inc(T &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
  return;
}
template <typename T> inline void Dec(T &x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
  return;
}
template <typename T> inline int fpow(int x, T k) {
  int ret = 1;
  for (; k; k >>= 1, x = (ll)x * x % mod)
    if (k & 1)
      ret = (ll)ret * x % mod;
  return ret;
}
int Sum(int x, int y) {
  x += y;
  if (x >= mod)
    return x - mod;
  return x;
}
int Dif(int x, int y) {
  x -= y;
  if (x < 0)
    return x + mod;
  return x;
}
int dp[N];
int C[N];
int n;
int lst[N];
int main() {
  init(n);
  dp[1] = 1;
  for (int i = 1; i <= n; ++i)
    init(C[i]);
  lst[C[1]] = 1;
  for (int i = 2; i <= n; ++i) {
    if (C[i - 1] == C[i])
      dp[i] = dp[i - 1];
    else
      dp[i] = Sum(dp[i - 1], dp[lst[C[i]]]);
    lst[C[i]] = i;
  }
  cout << dp[n] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define Set(a, b) memset(a, b, sizeof(a))
using namespace std;
const int N = 2e5 + 10;
const int mod = 1e9 + 7;
template <typename T> inline void init(T &x) {
  x = 0;
  char ch = getchar();
  bool t = 0;
  for (; ch > '9' || ch < '0'; ch = getchar())
    if (ch == '-')
      t = 1;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = (x << 1) + (x << 3) + (ch - 48);
  if (t)
    x = -x;
  return;
}
typedef long long ll;
typedef unsigned long long ull;
template <typename T> inline void Inc(T &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
  return;
}
template <typename T> inline void Dec(T &x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
  return;
}
template <typename T> inline int fpow(int x, T k) {
  int ret = 1;
  for (; k; k >>= 1, x = (ll)x * x % mod)
    if (k & 1)
      ret = (ll)ret * x % mod;
  return ret;
}
int Sum(int x, int y) {
  x += y;
  if (x >= mod)
    return x - mod;
  return x;
}
int Dif(int x, int y) {
  x -= y;
  if (x < 0)
    return x + mod;
  return x;
}
int dp[N];
int C[N];
int n;
int lst[N];
int main() {
  init(n);
  dp[1] = 1;
  for (int i = 1; i <= n; ++i)
    init(C[i]);
  lst[C[1]] = 1;
  for (int i = 2; i <= n; ++i) {
    if (C[i - 1] == C[i])
      dp[i] = dp[i - 1];
    else
      dp[i] = Sum(dp[i - 1], dp[lst[C[i]]]);
    lst[C[i]] = i;
  }
  cout << dp[n] << endl;
  return 0;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int a[100007];
int b[100007];
long long dp[100007];
long long sum[100007];
const long long mod = 1e9 + 7;
int main() {
  dp[0] = 1;
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] != a[i - 1])
      b[++cnt] = a[i];
  }
  for (int i = 1; i <= cnt; i++) {
    dp[i] = dp[i - 1];
    dp[i] = (dp[i] + sum[b[i]]) % mod;
    sum[b[i]] = (sum[b[i]] + dp[i - 1]) % mod;
  }
  printf("%lld", dp[cnt]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int a[200007];
int b[200007];
long long dp[200007];
long long sum[200007];
const long long mod = 1e9 + 7;
int main() {
  dp[0] = 1;
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] != a[i - 1])
      b[++cnt] = a[i];
  }
  for (int i = 1; i <= cnt; i++) {
    dp[i] = dp[i - 1];
    dp[i] = (dp[i] + sum[b[i]]) % mod;
    sum[b[i]] = (sum[b[i]] + dp[i - 1]) % mod;
  }
  printf("%lld", dp[cnt]);
  return 0;
}
Problem p03096 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mp make_pair
#define all(vec) vec.begin(), vec.end()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = 1LL << 30;
const ll LINF = 1LL << 60;
const double eps = 1e-9;
const ll MOD = 1000000007LL;
template <typename T> void chmin(T &a, T b) { a = min(a, b); };
template <typename T> void chmax(T &a, T b) { a = max(a, b); };
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
};
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int main() {
  int n;
  cin >> n;
  vector<int> c(n + 10), b(100010, -1);
  for (int i = 1; i <= n; i++)
    cin >> c[i];
  vector<ll> dp(n + 10);
  dp[0] = 1;
  for (int i = 1; i <= n; i++) {
    dp[i] += dp[i - 1];
    if (b[c[i]] != -1 && b[c[i]] != i - 1) {
      dp[i] += dp[b[c[i]]];
    }
    b[c[i]] = i;
    dp[i] %= MOD;
  }
  cout << dp[n] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define mp make_pair
#define all(vec) vec.begin(), vec.end()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = 1LL << 30;
const ll LINF = 1LL << 60;
const double eps = 1e-9;
const ll MOD = 1000000007LL;
template <typename T> void chmin(T &a, T b) { a = min(a, b); };
template <typename T> void chmax(T &a, T b) { a = max(a, b); };
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
};
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int main() {
  int n;
  cin >> n;
  vector<int> c(n + 10), b(200010, -1);
  for (int i = 1; i <= n; i++)
    cin >> c[i];
  vector<ll> dp(n + 10);
  dp[0] = 1;
  for (int i = 1; i <= n; i++) {
    dp[i] += dp[i - 1];
    if (b[c[i]] != -1 && b[c[i]] != i - 1) {
      dp[i] += dp[b[c[i]]];
    }
    b[c[i]] = i;
    dp[i] %= MOD;
  }
  cout << dp[n] << endl;
}
Problem p03096 (C++)

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

using ll = long long;
#define MOD 1000000007

int N;
vector<int> C;
vector<int> memo;
vector<int> nearest;
vector<int> ind;

int f(int i) {
  if (i >= nearest.size())
    return 1;
  if (memo[i] != -1)
    return memo[i];
  if (nearest[i] == -1) {
    return memo[i] = f(i + 1) % MOD;
  } else {
    return memo[i] = (f(nearest[i]) + f(i + 1)) % MOD;
  }
}

int main() {
  cin >> N;
  C.resize(N);
  for (auto &e : C)
    cin >> e;
  C.erase(unique(C.begin(), C.end()), C.end());

  ind.assign(N + 1, -1);
  nearest.assign(C.size(), -1);
  for (int i = C.size() - 1; i >= 0; i--) {
    if (ind[C[i]] != -1)
      nearest[i] = ind[C[i]];
    ind[C[i]] = i;
  }

  memo.assign(C.size(), -1);
  ll ans = f(0);
  cout << ans << endl;
}


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

using ll = long long;
#define MOD 1000000007

int N;
vector<int> C;
vector<int> memo;
vector<int> nearest;
vector<int> ind;

int f(int i) {
  if (i >= nearest.size())
    return 1;
  if (memo[i] != -1)
    return memo[i];
  if (nearest[i] == -1) {
    return memo[i] = f(i + 1) % MOD;
  } else {
    return memo[i] = (f(nearest[i]) + f(i + 1)) % MOD;
  }
}

int main() {
  cin >> N;
  C.resize(N);
  for (auto &e : C)
    cin >> e;
  C.erase(unique(C.begin(), C.end()), C.end());

  ind.assign(200000 + 1, -1);
  nearest.assign(C.size(), -1);
  for (int i = C.size() - 1; i >= 0; i--) {
    if (ind[C[i]] != -1)
      nearest[i] = ind[C[i]];
    ind[C[i]] = i;
  }

  memo.assign(C.size(), -1);
  ll ans = f(0);
  cout << ans << endl;
}
Problem p03097 (C++)

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

int n;
int dfs(int pos, int cnt, int a, int bit, vector<int> &tmp) {
  int keta = (pos + cnt) % n;
  if (cnt == 0) {
    if ((bit >> cnt) & 1)
      return a;
    else {
      a ^= (1 << keta);
      tmp.push_back(a);
      return a;
    }
  } else {
    if ((bit >> cnt) & 1)
      return dfs(pos, cnt - 1, a, bit, tmp);
    else {
      a = dfs(pos, cnt - 1, a, bit, tmp);
      a ^= (1 << keta);
      tmp.push_back(a);
      a = dfs(pos, cnt - 1, a, bit, tmp);
      return a;
    }
  }
}
void solve(int a, int b, int bit, vector<int> &v) {
  if (a == b) {
    v.push_back(b);
    return;
  }
  for (int i = 0; i < n; i++) {
    if (((a ^ b) >> i) & 1) {
      bit ^= (1 << i);
      for (int j = 0; j < n; j++) {
        vector<int> tmp;
        tmp.push_back(a);
        int x = dfs(j, 0, a, bit, tmp);
        if (x != b) {
          v.insert(v.end(), tmp.begin(), tmp.end());
          solve(x ^ (1 << i), b, bit, v);
          return;
        }
      }
    }
  }
}
int main() {
  int a, b;
  cin >> n >> a >> b;
  if ((__builtin_popcount(a ^ b) & 1) == 0) {
    cout << "NO" << endl;
    return 0;
  }
  vector<int> v;
  int bit = 0;
  solve(a, b, bit, v);
  cout << "YES" << endl;
  for (int i = 0; i < (1 << n); i++) {
    cout << v[i] << ((i + 1 == (1 << n)) ? "\n" : " ");
  }
  return 0;
}

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

int n;
int dfs(int pos, int cnt, int a, int bit, vector<int> &tmp) {
  int keta = (pos + cnt) % n;
  if (cnt == 0) {
    if ((bit >> cnt) & 1)
      return a;
    else {
      a ^= (1 << keta);
      tmp.push_back(a);
      return a;
    }
  } else {
    if ((bit >> cnt) & 1)
      return dfs(pos, cnt - 1, a, bit, tmp);
    else {
      a = dfs(pos, cnt - 1, a, bit, tmp);
      a ^= (1 << keta);
      tmp.push_back(a);
      a = dfs(pos, cnt - 1, a, bit, tmp);
      return a;
    }
  }
}
void solve(int a, int b, int bit, vector<int> &v) {
  if (a == b) {
    v.push_back(b);
    return;
  }
  for (int i = 0; i < n; i++) {
    if (((a ^ b) >> i) & 1) {
      bit ^= (1 << i);
      for (int j = 0; j < n; j++) {
        vector<int> tmp;
        tmp.push_back(a);
        int x = dfs(j, n - 1, a, bit, tmp);
        if (x != b) {
          v.insert(v.end(), tmp.begin(), tmp.end());
          solve(x ^ (1 << i), b, bit, v);
          return;
        }
      }
    }
  }
}
int main() {
  int a, b;
  cin >> n >> a >> b;
  if ((__builtin_popcount(a ^ b) & 1) == 0) {
    cout << "NO" << endl;
    return 0;
  }
  vector<int> v;
  int bit = 0;
  solve(a, b, bit, v);
  cout << "YES" << endl;
  for (int i = 0; i < (1 << n); i++) {
    cout << v[i] << ((i + 1 == (1 << n)) ? "\n" : " ");
  }
  return 0;
}
Problem p03097 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
const int N = 1e6;
int n, A, B;
int a[N];
int count(int x) {
  int sum = 0;
  while (x) {
    sum += (x & 1);
    x >>= 1;
  }
  return sum;
}
void work(int *a, int n, int A, int B) {
  if (!n) {
    a[0] = A;
    return;
  }
  if (n == 1) {
    a[0] = A;
    a[1] = B;
    return;
  }
  int sig = 0;
  int AA, BB;
  int x;
  while (1) {
    while (((1 << sig) & A) == ((1 << sig) & B))
      sig++;
    AA = (A & ((1 << (sig)) - 1)) + ((A >> (sig + 1)) << (sig));
    BB = ((B & ((1 << (sig)) - 1)) + ((B >> (sig + 1)) << (sig)));
    x = 1;
    while ((AA ^ x) == BB && x <= (1 << (n - 2)))
      x <<= 1;
    if (x <= (1 << (n - 2)))
      break;
  }
  work(a, n - 1, AA, (AA ^ x));

  work(a + (1 << (n - 1)), n - 1, (AA ^ x), BB);
  for (int i = 0; i < (1 << (n - 1)); i++)
    a[i] = ((a[i] >> (sig)) << (sig + 1)) + (a[i] & ((1 << (sig)) - 1)) +
           (A & (1 << sig));
  for (int i = (1 << (n - 1)); i < (1 << n); i++)
    a[i] = ((a[i] >> (sig)) << (sig + 1)) + (a[i] & ((1 << (sig)) - 1)) +
           (B & (1 << sig));
}
int main() {
  scanf("%d %d %d", &n, &A, &B);
  if (count(A) == count(B)) {
    printf("NO");
    return 0;
  }
  work(a, n, A, B);
  printf("YES\n");
  for (int i = 0; i < (1 << n); i++)
    printf("%d ", a[i]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
const int N = 1e6;
int n, A, B;
int a[N];
int count(int x) {
  int sum = 0;
  while (x) {
    sum += (x & 1);
    x >>= 1;
  }
  return sum;
}
void work(int *a, int n, int A, int B) {
  if (!n) {
    a[0] = A;
    return;
  }
  if (n == 1) {
    a[0] = A;
    a[1] = B;
    return;
  }
  int sig = 0;
  int AA, BB;
  int x;
  while (1) {
    while (((1 << sig) & A) == ((1 << sig) & B))
      sig++;
    AA = (A & ((1 << (sig)) - 1)) + ((A >> (sig + 1)) << (sig));
    BB = ((B & ((1 << (sig)) - 1)) + ((B >> (sig + 1)) << (sig)));
    x = 1;
    while ((AA ^ x) == BB && x <= (1 << (n - 2)))
      x <<= 1;
    if (x <= (1 << (n - 2)))
      break;
  }
  work(a, n - 1, AA, (AA ^ x));

  work(a + (1 << (n - 1)), n - 1, (AA ^ x), BB);
  for (int i = 0; i < (1 << (n - 1)); i++)
    a[i] = ((a[i] >> (sig)) << (sig + 1)) + (a[i] & ((1 << (sig)) - 1)) +
           (A & (1 << sig));
  for (int i = (1 << (n - 1)); i < (1 << n); i++)
    a[i] = ((a[i] >> (sig)) << (sig + 1)) + (a[i] & ((1 << (sig)) - 1)) +
           (B & (1 << sig));
}
int main() {
  scanf("%d %d %d", &n, &A, &B);
  if ((count(A) & 1) == (count(B) & 1)) {
    printf("NO");
    return 0;
  }
  work(a, n, A, B);
  printf("YES\n");
  for (int i = 0; i < (1 << n); i++)
    printf("%d ", a[i]);
  return 0;
}