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

Buggy submission (status: Runtime Error):
// https://atcoder.jp/contests/sumitrust2019/tasks/sumitb2019_e
// E - Colorful Hats 2
#include <bits/stdc++.h>
#if LOCAL
#include "dump.hpp"
#else
#define dump(...)
#endif
using namespace std;
using ll = long long;
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
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 (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
constexpr ll MOD = 1e9 + 7;
int main() {
#if LOCAL & 01
  std::ifstream in("./test/sample-1.in"); // input.txt
  std::cin.rdbuf(in.rdbuf());
#else
  cin.tie(0);
  ios::sync_with_stdio(false);
#endif
  ll N;
  cin >> N;
  vector<ll> an(N);
  for (auto &&a : an) {
    cin >> a;
  }
  ll ans = 1;
  ll x = 0, y = 0, z = 0;
  FOR(i, 0, N) {
    auto c = an.at(i);
    ll mul = 0;
    if (x == c)
      ++mul;
    if (y == c)
      ++mul;
    if (z == c)
      ++mul;
    if (x == c) {
      ++x;
    } else if (y == c) {
      ++y;
    } else if (z == c) {
      ++z;
    } else {
      assert(0);
    }
    ans *= mul;
    ans %= MOD;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
// https://atcoder.jp/contests/sumitrust2019/tasks/sumitb2019_e
// E - Colorful Hats 2
#include <bits/stdc++.h>
#if LOCAL
#include "dump.hpp"
#else
#define dump(...)
#endif
using namespace std;
using ll = long long;
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
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 (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
constexpr ll MOD = 1e9 + 7;
int main() {
#if LOCAL & 01
  std::ifstream in("./test/sample-1.in"); // input.txt
  std::cin.rdbuf(in.rdbuf());
#else
  cin.tie(0);
  ios::sync_with_stdio(false);
#endif
  ll N;
  cin >> N;
  vector<ll> an(N);
  for (auto &&a : an) {
    cin >> a;
  }
  ll ans = 1;
  ll x = 0, y = 0, z = 0;
  FOR(i, 0, N) {
    auto c = an.at(i);
    ll mul = 0;
    if (x == c)
      ++mul;
    if (y == c)
      ++mul;
    if (z == c)
      ++mul;
    if (x == c) {
      ++x;
    } else if (y == c) {
      ++y;
    } else if (z == c) {
      ++z;
    } else {
      cout << 0 << endl;
      exit(0);
    }
    ans *= mul;
    ans %= MOD;
  }
  cout << ans << endl;
  return 0;
}
Problem p02845 (C++)

Buggy submission (status: Runtime Error):
// smtb2019_e.cpp
// Sun Dec  1 20:57:22 2019

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define INTINF 2147483647
#define LLINF 9223372036854775807
using namespace std;
using ll = long long;
typedef pair<int, int> P;
ll MOD = 1000000007;

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

  ll a[n];
  ll color[3];
  color[0] = 0;
  color[1] = 0;
  color[2] = 0;
  ll ans = 1;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    ll temp = 0;
    ll index = -1;
    for (int j = 0; j < 3; j++) {
      if (color[j] == a[i]) {
        temp++;
        index = j;
      }
    }
    ans = ans * temp % MOD;
    //		cout << index << endl;
    color[index]++;
  }

  cout << ans << endl;
  //	printf("%.4f\n",ans);
}

Fixed submission:
// smtb2019_e.cpp
// Sun Dec  1 20:57:22 2019

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define INTINF 2147483647
#define LLINF 9223372036854775807
using namespace std;
using ll = long long;
typedef pair<int, int> P;
ll MOD = 1000000007;

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

  ll a[n];
  ll color[3];
  color[0] = 0;
  color[1] = 0;
  color[2] = 0;
  ll ans = 1;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    ll temp = 0;
    ll index = -1;
    for (int j = 0; j < 3; j++) {
      if (color[j] == a[i]) {
        temp++;
        index = j;
      }
    }
    if (index == -1) {
      cout << 0 << endl;
      return 0;
    }
    ans = ans * temp % MOD;
    //		cout << index << endl;
    color[index]++;
  }

  cout << ans << endl;
  //	printf("%.4f\n",ans);
}
Problem p02845 (C++)

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

using namespace std;

const int mod = 1e9 + 7;

int add(int a, int b) { return a + b - mod * (a + b >= mod); }

int mul(int a, int b) { return int(1ll * a * b % mod); }

int power(int a, int x) {
  if (x == 0) {
    return 1;
  }
  return mul(power(mul(a, a), x >> 1), (x & 1 ? a : 1));
}

int divide(int a, int b) { return mul(a, power(b, mod - 2)); }

int sub(int a, int b) { return a - b + mod * (a < b); }

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

  int n;
  cin >> n;
  int cnt[3] = {0};
  int ans = 1;
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    bool was = false;
    int kek = 0;
    for (int j = 0; j < 3; ++j) {
      if (x == cnt[j]) {
        if (!was) {
          ++cnt[j];
          was = true;
        } else {
          kek = add(kek, ans);
        }
      }
    }
    assert(was);
    ans = add(ans, kek);
  }
  cout << ans;

  return 0;
}

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

using namespace std;

const int mod = 1e9 + 7;

int add(int a, int b) { return a + b - mod * (a + b >= mod); }

int mul(int a, int b) { return int(1ll * a * b % mod); }

int power(int a, int x) {
  if (x == 0) {
    return 1;
  }
  return mul(power(mul(a, a), x >> 1), (x & 1 ? a : 1));
}

int divide(int a, int b) { return mul(a, power(b, mod - 2)); }

int sub(int a, int b) { return a - b + mod * (a < b); }

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

  int n;
  cin >> n;
  int cnt[3] = {0};
  int ans = 1;
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    bool was = false;
    int kek = 0;
    for (int j = 0; j < 3; ++j) {
      if (x == cnt[j]) {
        if (!was) {
          ++cnt[j];
          was = true;
        } else {
          kek = add(kek, ans);
        }
      }
    }
    if (!was) {
      cout << 0;
      return 0;
    }
    ans = add(ans, kek);
  }
  cout << ans;

  return 0;
}
Problem p02845 (C++)

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

template <typename T> void out(T x) {
  cout << x << endl;
  exit(0);
}
#define watch(x) cout << (#x) << " is " << (x) << endl

using ll = long long;

const ll mod = 1e9 + 7;
const int maxn = 1e6 + 5;

int n;
int a[maxn];

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

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

  multiset<int> ms;
  ms.insert(0);
  ms.insert(0);
  ms.insert(0);

  ll res = 1;

  for (int i = 0; i < n; i++) {
    res = res * ms.count(a[i]) % mod;
    ms.erase(ms.find(a[i]));
    ms.insert(a[i] + 1);
  }
  cout << res << endl;
  return 0;
}


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

template <typename T> void out(T x) {
  cout << x << endl;
  exit(0);
}
#define watch(x) cout << (#x) << " is " << (x) << endl

using ll = long long;

const ll mod = 1e9 + 7;
const int maxn = 1e6 + 5;

int n;
int a[maxn];

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

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

  multiset<int> ms;
  ms.insert(0);
  ms.insert(0);
  ms.insert(0);

  ll res = 1;

  for (int i = 0; i < n; i++) {
    res = res * ms.count(a[i]) % mod;
    if (res == 0)
      out(0);
    ms.erase(ms.find(a[i]));
    ms.insert(a[i] + 1);
  }
  cout << res << endl;
  return 0;
}
Problem p02845 (C++)

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

long long n;
long long a[100002];

long long md = 1000000007ll;

map<pair<pair<int, int>, pair<int, int>>, long long> mp;

long long slv(int id, int rd, int bl, int gr) {
  // cout<<id<<" "<<rd<<" "<<bl<<" "<<gr<<endl;
  if (id == -1) {
    if (rd <= 0 && bl <= 0 && gr <= 0)
      return 1ll;
    else
      return 0ll;
  }
  if (id + 1 < rd || id + 1 < bl || id + 1 < gr)
    return (0ll);

  pair<pair<int, int>, pair<int, int>> p =
      make_pair(make_pair(id, rd), make_pair(bl, gr));
  if (mp.count(p))
    return (mp[p]);

  long long ret = 0ll;

  // rd
  if (rd == -1 || rd - 1 == a[id]) {
    if (rd == -1)
      ret += slv(id - 1, a[id], bl, gr);
    else
      ret += slv(id - 1, rd - 1, bl, gr);
  }
  // bl
  if (bl == -1 || bl - 1 == a[id]) {
    if (bl == -1)
      ret += slv(id - 1, rd, a[id], gr);
    else
      ret += slv(id - 1, rd, bl - 1, gr);
  }

  // gr
  if (gr == -1 || gr - 1 == a[id]) {
    if (gr == -1)
      ret += slv(id - 1, rd, bl, a[id]);
    else
      ret += slv(id - 1, rd, bl, gr - 1);
  }

  return (mp[p] = ret % md);
}

int main() {

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

  long long ans = slv(n - 1, -1, -1, -1);
  cout << ans << endl;
}

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

long long n;
long long a[100002];

long long md = 1000000007ll;

map<pair<pair<int, int>, pair<int, int>>, long long> mp;

long long slv(int id, int rd, int bl, int gr) {
  // cout<<id<<" "<<rd<<" "<<bl<<" "<<gr<<endl;
  if (id == -1) {
    if (rd <= 0 && bl <= 0 && gr <= 0)
      return 1ll;
    else
      return 0ll;
  }
  if (id + 1 < rd || id + 1 < bl || id + 1 < gr)
    return (0ll);
  long long hoge = 0;
  if (rd != -1)
    hoge += rd;
  if (bl != -1)
    hoge += bl;
  if (gr != -1)
    hoge += gr;
  if (id + 1 < hoge)
    return (0ll);

  pair<pair<int, int>, pair<int, int>> p =
      make_pair(make_pair(id, rd), make_pair(bl, gr));
  if (mp.count(p))
    return (mp[p]);

  long long ret = 0ll;

  // rd
  if (rd == -1 || rd - 1 == a[id]) {
    if (rd == -1)
      ret += slv(id - 1, a[id], bl, gr);
    else
      ret += slv(id - 1, rd - 1, bl, gr);
  }
  // bl
  if (bl == -1 || bl - 1 == a[id]) {
    if (bl == -1)
      ret += slv(id - 1, rd, a[id], gr);
    else
      ret += slv(id - 1, rd, bl - 1, gr);
  }

  // gr
  if (gr == -1 || gr - 1 == a[id]) {
    if (gr == -1)
      ret += slv(id - 1, rd, bl, a[id]);
    else
      ret += slv(id - 1, rd, bl, gr - 1);
  }

  return (mp[p] = ret % md);
}

int main() {

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

  long long ans = slv(n - 1, -1, -1, -1);
  cout << ans << endl;
}
Problem p02845 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
typedef pair<ll, ll> P;
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
const ll inf = (1e18);
const ll mod = 1000000007;
// ios_base::sync_with_stdio(false);
// cin.tie(NULL);
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll c, ll d) { return c / gcd(c, d) * d; }
struct __INIT {
  __INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
  }
} __init;
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;
}
int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  multiset<ll> s;
  s.insert(0);
  s.insert(0);
  s.insert(0);
  ll ans = 1;
  for (int i = 0; i < n; i++) {
    ll t = s.count(a[i]);
    (ans *= t) %= mod;
    s.erase(s.find(a[i]));
    s.insert(a[i] + 1);
  }
  cout << ans << endl;
}


Fixed submission:
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
typedef pair<ll, ll> P;
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
const ll inf = (1e18);
const ll mod = 1000000007;
// ios_base::sync_with_stdio(false);
// cin.tie(NULL);
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll c, ll d) { return c / gcd(c, d) * d; }
struct __INIT {
  __INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
  }
} __init;
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;
}
int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  multiset<ll> s;
  s.insert(0);
  s.insert(0);
  s.insert(0);
  ll ans = 1;
  for (int i = 0; i < n; i++) {
    ll t = s.count(a[i]);
    if (t == 0) {
      cout << 0 << endl;
      return 0;
    }
    (ans *= t) %= mod;
    s.erase(s.find(a[i]));
    s.insert(a[i] + 1);
  }
  cout << ans << endl;
}
Problem p02845 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

using namespace std;

// vector string deque break continue
#define forn(i, s, f) for (int i = (int)s; i < (int)f; i++)
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int, int>
#define fs first
#define sc second
#define pf push_front
#define pb push_back
#define pop_f pop_front
#define pop_b pop_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()

const int MOD = 1e9 + 7;

int add(int x, int y) {
  x += y;
  if (x >= MOD)
    return x - MOD;
  return x;
}

int sub(int x, int y) {
  x -= y;
  if (x < 0)
    return x + MOD;
  return x;
}

int mul(int x, int y) { return (1LL * x * y) % MOD; }

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  int n;
  cin >> n;
  int ans = 1;
  multiset<int> a;
  forn(i, 0, 3) a.insert(-1);
  forn(i, 0, n) {
    int t;
    cin >> t;
    ans = mul(ans, a.count(t - 1));
    a.erase(a.find(t - 1));
    a.insert(t);
  }
  cout << ans;
  return 0;
}

Fixed submission:
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

using namespace std;

// vector string deque break continue
#define forn(i, s, f) for (int i = (int)s; i < (int)f; i++)
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int, int>
#define fs first
#define sc second
#define pf push_front
#define pb push_back
#define pop_f pop_front
#define pop_b pop_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()

const int MOD = 1e9 + 7;

int add(int x, int y) {
  x += y;
  if (x >= MOD)
    return x - MOD;
  return x;
}

int sub(int x, int y) {
  x -= y;
  if (x < 0)
    return x + MOD;
  return x;
}

int mul(int x, int y) { return (1LL * x * y) % MOD; }

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  int n;
  cin >> n;
  int ans = 1;
  multiset<int> a;
  forn(i, 0, 3) a.insert(-1);
  forn(i, 0, n) {
    int t;
    cin >> t;
    ans = mul(ans, a.count(t - 1));
    if (a.count(t - 1) == 0) {
      return cout << ans, 0;
    }
    a.erase(a.find(t - 1));
    a.insert(t);
  }
  cout << ans;
  return 0;
}
Problem p02845 (C++)

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

using namespace std;

long long int MOD = 1000000007;

int N;
int A[100005];
multiset<int> S;

long long int dp[100005];

int main(void) {

  cin >> N;

  dp[0] = 1;

  for (int i = 1; i <= N; i++) {
    cin >> A[i];
  }

  for (int i = 0; i <= 2; i++) {
    S.insert(0);
  }

  for (int i = 1; i <= N; i++) {
    long long int num = S.count(A[i]);
    dp[i] = (dp[i - 1] * num) % MOD;
    auto it = S.find(A[i]);
    S.erase(it);
    S.insert(A[i] + 1);
  }
  cout << dp[N] << endl;
  return 0;
}

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

using namespace std;

long long int MOD = 1000000007;

int N;
int A[100005];
multiset<int> S;

long long int dp[100005];

int main(void) {

  cin >> N;

  dp[0] = 1;

  for (int i = 1; i <= N; i++) {
    cin >> A[i];
  }

  for (int i = 0; i <= 2; i++) {
    S.insert(0);
  }

  for (int i = 1; i <= N; i++) {
    long long int num = S.count(A[i]);
    if (num == 0) {
      cout << 0 << endl;
      return 0;
    }
    dp[i] = (dp[i - 1] * num) % MOD;
    auto it = S.find(A[i]);
    S.erase(it);
    S.insert(A[i] + 1);
  }
  cout << dp[N] << endl;
  return 0;
}
Problem p02845 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cerr << #x << ": " << x << endl
const ll mod = 1000000007;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];

  vector<int> k(n);
  int s[3] = {0, 0, 0};
  rep(i, n) {
    int cnt = 0;
    int nc = 4;
    rep(col, 3) {
      if (s[col] == a[i]) {
        cnt++;
        nc = min(nc, col);
      }
    }
    k[i] = cnt;
    s[nc]++;
  }
  ll ans = 1LL;
  rep(i, n) ans = (ans * k[i]) % mod;
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cerr << #x << ": " << x << endl
const ll mod = 1000000007;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];

  vector<int> k(n);
  int s[3] = {0, 0, 0};
  rep(i, n) {
    int cnt = 0;
    int nc = 4;
    rep(col, 3) {
      if (s[col] == a[i]) {
        cnt++;
        nc = min(nc, col);
      }
    }
    k[i] = cnt;
    if (nc > 3)
      break;
    dbg(nc);
    s[nc]++;
  }
  ll ans = 1LL;
  rep(i, n) ans = (ans * k[i]) % mod;
  cout << ans << endl;
  return 0;
}
Problem p02845 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define FOR_LT(i, beg, end) for (int i = beg; i < end; i++)
#define FOR_LE(i, beg, end) for (int i = beg; i <= end; i++)
#define FOR_DW(i, beg, end) for (int i = beg; end <= i; i--)

using namespace std;

int64_t kRem = 1000000007;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(20);

  int n;
  cin >> n;
  int nums[3] = {-1, -1, -1};

  int64_t ans = 1;
  FOR_LT(i, 0, n) {
    int num;
    cin >> num;
    int64_t c = 0;
    bool sumi = false;
    FOR_LT(j, 0, 3) {
      if (nums[j] == num - 1) {
        c++;
        if (!sumi) {
          nums[j]++;
          sumi = true;
        }
      }
    }
    assert(sumi);
    ans *= c;
    ans %= kRem;
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define FOR_LT(i, beg, end) for (int i = beg; i < end; i++)
#define FOR_LE(i, beg, end) for (int i = beg; i <= end; i++)
#define FOR_DW(i, beg, end) for (int i = beg; end <= i; i--)

using namespace std;

int64_t kRem = 1000000007;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(20);

  int n;
  cin >> n;
  int nums[3] = {-1, -1, -1};

  int64_t ans = 1;
  FOR_LT(i, 0, n) {
    int num;
    cin >> num;
    int64_t c = 0;
    bool sumi = false;
    FOR_LT(j, 0, 3) {
      if (nums[j] == num - 1) {
        c++;
        if (!sumi) {
          nums[j]++;
          sumi = true;
        }
      }
    }
    if (!sumi) {
      cout << 0 << endl;
      return 0;
    }
    ans *= c;
    ans %= kRem;
  }

  cout << ans << endl;

  return 0;
}
Problem p02845 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#define rep(i, n) for (ll i = 0; i < n; i++)
typedef long long ll;
using namespace std;
#define llMAX numeric_limits<long long>::max()
#define intMAX numeric_limits<int>::max()
#define d_5 100000
#define d9_7 1000000007
#define vll vector<vector<long long>>
#define vl vector<long long>
#define vi vector<int>
#define vii vector<vector<int>>
#define pb push_back
#define pf push_front
#define ld long double
#define ll long long
int main(void) {
  ll n;
  cin >> n;
  vl a(n);
  rep(i, n) { cin >> a[i]; }
  vll dp = vll(3, vl(n, 0));
  dp[0][0] = 1;
  vl dp2(n, 1); // 結び目の個数
  dp2[0] = 3;
  ll ans = 1;
  if (n <= 1) {
    a[1000000] = 0;
  }
  for (ll i = 1; i < n; i++) {
    ll num = a[i];
    ll cross = 0;
    if (dp[0][i - 1] == num)
      cross += 1;
    ;
    if (dp[1][i - 1] == num)
      cross += 1;
    ;
    if (dp[2][i - 1] == num)
      cross += 1;
    ;
    dp2[i] = cross;
    if (cross == 0) {
      cout << 0 << endl;
      return 0;
    }
    rep(j, 3) { dp[j][i] = dp[j][i - 1]; }
    rep(k, 3) {
      if (dp[k][i - 1] == num) {
        dp[k][i] = num + 1;
        break;
      }
    }
  }
  rep(i, n) {
    ans *= dp2[i];
    ans %= d9_7;
  }
  cout << ans % d9_7 << endl;
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
#define rep(i, n) for (ll i = 0; i < n; i++)
typedef long long ll;
using namespace std;
#define llMAX numeric_limits<long long>::max()
#define intMAX numeric_limits<int>::max()
#define d_5 100000
#define d9_7 1000000007
#define vll vector<vector<long long>>
#define vl vector<long long>
#define vi vector<int>
#define vii vector<vector<int>>
#define pb push_back
#define pf push_front
#define ld long double
#define ll long long
int main(void) {
  ll n;
  cin >> n;
  vl a(n);
  rep(i, n) { cin >> a[i]; }
  vll dp = vll(3, vl(n, 0));
  dp[0][0] = 1;
  vl dp2(n, 1); // 結び目の個数
  dp2[0] = 3;
  ll ans = 1;
  if (a[0] != 0) {
    cout << 0;
    return 0;
  }
  if (n == 2) {
    if (a[1] == 1) {
      cout << 3;
    } else if (a[1] == 0) {
      cout << 6;
    } else {
      cout << 0;
    }
    return 0;
  } else if (n == 1) {
    cout << 3;
    return 0;
  }
  for (ll i = 1; i < n; i++) {
    ll num = a[i];
    ll cross = 0;
    if (dp[0][i - 1] == num)
      cross += 1;
    ;
    if (dp[1][i - 1] == num)
      cross += 1;
    ;
    if (dp[2][i - 1] == num)
      cross += 1;
    ;
    dp2[i] = cross;
    if (cross == 0) {
      cout << 0 << endl;
      return 0;
    }
    rep(j, 3) { dp[j][i] = dp[j][i - 1]; }
    rep(k, 3) {
      if (dp[k][i - 1] == num) {
        dp[k][i] = num + 1;
        break;
      }
    }
  }
  rep(i, n) {
    ans *= dp2[i];
    ans %= d9_7;
  }
  cout << ans % d9_7 << endl;
  return 0;
}
Problem p02845 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef long double D;
typedef complex<D> P;
#define F first
#define S second
const ll MOD = 1000000007;
// const ll MOD=998244353;

template <typename T, typename U>
istream &operator>>(istream &i, pair<T, U> &A) {
  i >> A.F >> A.S;
  return i;
}
template <typename T> istream &operator>>(istream &i, vector<T> &A) {
  for (auto &I : A) {
    i >> I;
  }
  return i;
}
template <typename T, typename U>
ostream &operator<<(ostream &o, const pair<T, U> &A) {
  o << A.F << " " << A.S;
  return o;
}
template <typename T> ostream &operator<<(ostream &o, const vector<T> &A) {
  ll i = A.size();
  for (auto &I : A) {
    o << I << (--i ? " " : "");
  }
  return o;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N;
  cin >> N;
  vector<ll> A(N, 0);
  cin >> A;
  vector<ll> cnt(3, 0);
  ll ans = 1;
  for (auto &I : A) {
    ll c = 0, idx = -1;
    for (int j = 0; j < 3; j++) {
      if (cnt[j] == I) {
        c++;
        idx = j;
      }
    }
    ans *= c;
    cnt[idx]++;
    ans %= MOD;
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef long double D;
typedef complex<D> P;
#define F first
#define S second
const ll MOD = 1000000007;
// const ll MOD=998244353;

template <typename T, typename U>
istream &operator>>(istream &i, pair<T, U> &A) {
  i >> A.F >> A.S;
  return i;
}
template <typename T> istream &operator>>(istream &i, vector<T> &A) {
  for (auto &I : A) {
    i >> I;
  }
  return i;
}
template <typename T, typename U>
ostream &operator<<(ostream &o, const pair<T, U> &A) {
  o << A.F << " " << A.S;
  return o;
}
template <typename T> ostream &operator<<(ostream &o, const vector<T> &A) {
  ll i = A.size();
  for (auto &I : A) {
    o << I << (--i ? " " : "");
  }
  return o;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N;
  cin >> N;
  vector<ll> A(N, 0);
  cin >> A;
  vector<ll> cnt(3, 0);
  ll ans = 1;
  for (auto &I : A) {
    ll c = 0, idx = -1;
    for (int j = 0; j < 3; j++) {
      if (cnt[j] == I) {
        c++;
        idx = j;
      }
    }
    if (c == 0) {
      ans = 0;
      break;
    }
    ans *= c;
    cnt[idx]++;
    ans %= MOD;
  }
  cout << ans << endl;

  return 0;
}
Problem p02845 (C++)

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

#define rep(i, n) for (int i = 0; i < (n); ++i)

typedef long long ll;
typedef pair<int, int> P;

const int MAX = 1e5 + 5;
const int INF = 1001001001;
const int MOD = 1e9 + 7;

int main() {
  int N;
  cin >> N;

  vector<int> A(N);
  int maxA = 0;
  rep(i, N) {
    cin >> A[i];
    maxA = max(maxA, A[i]);
  }

  vector<int> dp(maxA + 1);
  dp[0] = 3;
  ll ans = 1;

  rep(i, N) {
    int a = A[i];
    ans *= dp[a];
    ans %= MOD;
    dp[a]--;
    dp[a + 1]++;
  }

  cout << ans << endl;
}

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

#define rep(i, n) for (int i = 0; i < (n); ++i)

typedef long long ll;
typedef pair<int, int> P;

const int MAX = 1e5 + 5;
const int INF = 1001001001;
const int MOD = 1e9 + 7;

int main() {
  int N;
  cin >> N;

  vector<int> A(N);
  int maxA = 0;
  rep(i, N) {
    cin >> A[i];
    maxA = max(maxA, A[i]);
  }

  vector<int> dp(1e5 + 10);
  dp[0] = 3;
  ll ans = 1;

  rep(i, N) {
    int a = A[i];
    ans *= dp[a];
    ans %= MOD;
    dp[a]--;
    dp[a + 1]++;
  }

  cout << ans << endl;
}
Problem p02845 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define vll vector<ll>
#define vi vector<int>
#define vpll vector<pair<ll, ll>>
#define pii pair<int, int>
#define REP(i, a) for (int(i) = 0; (i) < (a); (i)++)
#define RREP(i, a) for (int(i) = a - 1; (i) >= (0); (i)--)
#define REP2(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define MOD 1000000007
#define ALL(v) v.begin(), v.end()
#define m0(x) memset(x, 0, sizeof(x))
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;
}
typedef long long ll;

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll N;
  cin >> N;
  vector<ll> A(N);
  REP(i, N) cin >> A[i];
  vector<ll> color(3, 0);
  ll ans = 1, id = -1, kouho = 0;
  REP(i, N) {
    id = -1;
    kouho = 0;
    REP(j, 3) {
      if (A[i] == color[j]) {
        kouho++;
        id = j;
      }
    }
    color[id]++;
    ans *= kouho;
    ans %= MOD;
  }
  cout << ans;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define vll vector<ll>
#define vi vector<int>
#define vpll vector<pair<ll, ll>>
#define pii pair<int, int>
#define REP(i, a) for (int(i) = 0; (i) < (a); (i)++)
#define RREP(i, a) for (int(i) = a - 1; (i) >= (0); (i)--)
#define REP2(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define MOD 1000000007
#define ALL(v) v.begin(), v.end()
#define m0(x) memset(x, 0, sizeof(x))
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;
}
typedef long long ll;

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll N;
  cin >> N;
  vector<ll> A(N);
  REP(i, N) cin >> A[i];
  vector<ll> color(3, 0);
  ll ans = 1, id = -1, kouho = 0;
  REP(i, N) {
    id = -1;
    kouho = 0;
    REP(j, 3) {
      if (A[i] == color[j]) {
        kouho++;
        id = j;
      }
    }
    if (id == -1) {
      cout << "0" << endl;
      return 0;
    }
    color[id]++;
    ans *= kouho;
    ans %= MOD;
  }
  cout << ans;

  return 0;
}
Problem p02845 (Python)

Buggy submission (status: Runtime Error):
import sys

input = sys.stdin.readline

n = int(input())
a = list(map(int, input().split()))
rbg = [-1, -1, -1]
ans = 1
mod = 10**9 + 7

for A in a:
    ans *= rbg.count(A - 1)
    ans %= mod
    rbg[rbg.index(A - 1)] = A

print(ans)


Fixed submission:
import sys

input = sys.stdin.readline

n = int(input())
a = list(map(int, input().split()))
rbg = [-1, -1, -1]
ans = 1
mod = 10**9 + 7

for A in a:
    ans *= rbg.count(A - 1)
    ans %= mod
    for i in range(3):
        if rbg[i] == A - 1:
            rbg[i] = A
            break

print(ans)
Problem p02845 (Python)

Buggy submission (status: Runtime Error):
import sys

readline = sys.stdin.buffer.readline
n = int(readline())
A = map(int, readline().split())
mod = 1000000007

caps = [0] * 3
ans = 1
for a in A:
    ans *= caps.count(a)
    caps[caps.index(a)] += 1
    ans %= mod
print(ans)


Fixed submission:
import sys

readline = sys.stdin.buffer.readline
n = int(readline())
A = map(int, readline().split())
mod = 1000000007

caps = [0] * 3
ans = 1
for a in A:
    ans *= caps.count(a)
    # caps[caps.index(a)] += 1
    for i in range(3):
        if caps[i] == a:
            caps[i] += 1
            break
    ans %= mod
print(ans)
Problem p02845 (Python)

Buggy submission (status: Runtime Error):
n = int(input())
a = list(map(int, input().split()))
mod = 10**9 + 7

ans = 1
cnt = [0, 0, 0]
for i in a:
    ans *= cnt.count(i)
    cnt[cnt.index(i)] += 1
    ans %= mod
print(ans)


Fixed submission:
n = int(input())
a = list(map(int, input().split()))
mod = 10**9 + 7

ans = 1
cnt = [0, 0, 0]
for i in a:
    ans *= cnt.count(i)
    for j in range(3):
        if cnt[j] == i:
            cnt[j] += 1
            break
    ans %= mod
print(ans)
Problem p02845 (C++)

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

#define mod 1000000007

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

  ll ans = 1;
  multiset<int> se{0, 0, 0};
  for (int i = 0; i < N; ++i) {
    ans *= se.count(A[i]);
    ans %= mod;
    auto iter = se.lower_bound(A[i]);
    se.erase(iter);
    se.insert(A[i] + 1);
  }
  cout << ans << "\n";
  return 0;
}

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

#define mod 1000000007

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

  ll ans = 1;
  multiset<int> se{0, 0, 0};
  for (int i = 0; i < N; ++i) {
    ans *= se.count(A[i]);
    if (se.count(A[i]) == 0) {
      cout << 0 << "\n";
      return 0;
    }
    ans %= mod;
    auto iter = se.lower_bound(A[i]);
    se.erase(iter);
    se.insert(A[i] + 1);
  }
  cout << ans << "\n";
  return 0;
}
Problem p02845 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
long long mod = 1000000007;
const int INF = 1001001001;

int main() {
  ll n;
  cin >> n;
  ll ans = 1;
  vector<ll> count(40000, 0);
  for (ll i = 0; i < n; i++) {
    ll scope;
    cin >> scope;
    if (scope == 0) {
      ans = ans * (3 - (count.at(scope))) % mod;
    } else {
      ans = ans * (count.at(scope - 1) - count.at(scope)) % mod;
    }
    count.at(scope)++;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
long long mod = 1000000007;
const int INF = 1001001001;

int main() {
  ll n;
  cin >> n;
  ll ans = 1;
  vector<ll> count(100001, 0);
  for (ll i = 0; i < n; i++) {
    ll scope;
    cin >> scope;
    if (scope == 0) {
      ans = ans * (3 - (count.at(scope))) % mod;
    } else {
      ans = ans * (count.at(scope - 1) - count.at(scope)) % mod;
    }
    count.at(scope)++;
  }
  cout << ans << endl;
}
Problem p02845 (C++)

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

using namespace std;
using ll = long long;
using ld = long double;

const int mod = 1000000007;
// const int mod = 998244353;

// #define int long long
// signed main(){
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n;
  cin >> n;
  ll a[n];
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll cnt[3][n + 1];
  cnt[0][0] = 0;
  cnt[1][0] = 0;
  cnt[2][0] = 0;

  for (ll i = 0; i < n; i++) {
    ll c = 0;
    while (cnt[c][i] != a[i]) {
      c++;
    }
    for (ll j = 0; j < 3; j++)
      cnt[j][i + 1] = cnt[j][i];
    cnt[c][i + 1]++;
  }

  for (ll i = 0; i < 3; i++) {
    for (ll j = 0; j < n; j++) {
      cerr << std::right << std::setw(2) << cnt[i][j];
    }
    cerr << "\n";
  }

  ll ans = 1;
  for (ll i = 0; i < n; i++) {
    ll tmp = 0;
    for (ll j = 0; j < 3; j++)
      if (a[i] == cnt[j][i])
        tmp++;
    cerr << std::right << std::setw(2) << tmp;
    ans = (ans * tmp) % mod;
  }
  cout << ans << "\n";
  return 0;
}

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

using namespace std;
using ll = long long;
using ld = long double;

const int mod = 1000000007;
// const int mod = 998244353;

// #define int long long
// signed main(){
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n;
  cin >> n;
  ll a[n];
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll cnt[3][n + 1];
  cnt[0][0] = 0;
  cnt[1][0] = 0;
  cnt[2][0] = 0;

  for (ll i = 0; i < n; i++) {
    ll c = 0;
    while (cnt[c][i] != a[i]) {
      c++;
      if (c == 3) {
        cout << "0"
             << "\n";
        return 0;
      }
    }
    for (ll j = 0; j < 3; j++)
      cnt[j][i + 1] = cnt[j][i];
    cnt[c][i + 1]++;
  }

  for (ll i = 0; i < 3; i++) {
    for (ll j = 0; j < n; j++) {
      cerr << std::right << std::setw(2) << cnt[i][j];
    }
    cerr << "\n";
  }

  ll ans = 1;
  for (ll i = 0; i < n; i++) {
    ll tmp = 0;
    for (ll j = 0; j < 3; j++)
      if (a[i] == cnt[j][i])
        tmp++;
    cerr << std::right << std::setw(2) << tmp;
    ans = (ans * tmp) % mod;
  }
  cout << ans << "\n";
  return 0;
}
Problem p02845 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
constexpr int kMod = int(1E9 + 7);
constexpr int kN = int(1E5 + 10);
int a[kN];
int main() {
  int n, cnt, x = 0, y = 0, z = 0;
  long long int ans = 1;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) {
    cnt = 0;
    if (x == a[i])
      cnt++;
    if (y == a[i])
      cnt++;
    if (z == a[i])
      cnt++;
    ans = (ans * cnt) % kMod;
    if (x == a[i])
      x++;
    else if (y == a[i])
      y++;
    else if (z == a[i])
      z++;
    else
      a[kMod] = kN;
  }
  printf("%lld\n", ans);
}


Fixed submission:
#include <stdio.h>
constexpr int kMod = int(1E9 + 7);
constexpr int kN = int(1E5 + 10);
int a[kN];
int main() {
  int n, cnt, x = 0, y = 0, z = 0;
  long long int ans = 1;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) {
    cnt = 0;
    if (x == a[i])
      cnt++;
    if (y == a[i])
      cnt++;
    if (z == a[i])
      cnt++;
    ans = (ans * cnt) % kMod;
    if (x == a[i])
      x++;
    else if (y == a[i])
      y++;
    else if (z == a[i])
      z++;
    // else a[kMod] = kN;
  }
  printf("%lld\n", ans);
}
Problem p02845 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
using namespace literals;
constexpr int Mod = 1000000007;
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<int> table(N / 3 + 2, 0);
  table[0] = 3;
  int64_t ans = 1;
  for (int i = 0; i < N; ++i) {
    int A;
    cin >> A;
    ans = ans * table[A] % Mod;
    --table[A];
    ++table[A + 1];
  }
  cout << ans << endl;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
using namespace literals;
constexpr int Mod = 1000000007;
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<int> table(N + 2, 0);
  table[0] = 3;
  int64_t ans = 1;
  for (int i = 0; i < N; ++i) {
    int A;
    cin >> A;
    ans = ans * table[A] % Mod;
    --table[A];
    ++table[A + 1];
  }
  cout << ans << endl;
}
Problem p02845 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define eb emplace_back
#define mp make_pair
using namespace std;
multiset<int> s;
int n, x;
ll ans = 1;
const int mod = 1e9 + 7;
int main() {
  cin >> n;
  s.insert(0), s.insert(0), s.insert(0);
  for (int i = 0; i < n; i++) {
    cin >> x;
    (ans *= s.count(x)) %= mod;
    s.erase(s.find(x));
    s.insert(x + 1);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define eb emplace_back
#define mp make_pair
using namespace std;
multiset<int> s;
int n, x;
ll ans = 1;
const int mod = 1e9 + 7;
int main() {
  cin >> n;
  s.insert(0), s.insert(0), s.insert(0);
  for (int i = 0; i < n; i++) {
    cin >> x;
    (ans *= s.count(x)) %= mod;
    if (s.count(x)) {
      s.erase(s.find(x));
      s.insert(x + 1);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02845 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vc = vector<char>;
using vvc = vector<vc>;
using pll = pair<ll, ll>;
using stkll = vector<pll>;
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define debug(x) cerr << #x << ": " << x << endl;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

int main() {
  ll n;
  cin >> n;
  vll v = {-1, -1, -1};
  ll ans = 1;
  rep(i, n) {
    ll a;
    cin >> a;
    a--;
    ll cnt =
        upper_bound(v.begin(), v.end(), a) - lower_bound(v.begin(), v.end(), a);
    ans *= cnt;
    ans %= MOD;
    sort(v.begin(), v.end());
    *lower_bound(v.begin(), v.end(), a) = a + 1;
    sort(v.begin(), v.end());
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vc = vector<char>;
using vvc = vector<vc>;
using pll = pair<ll, ll>;
using stkll = vector<pll>;
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define debug(x) cerr << #x << ": " << x << endl;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

int main() {
  ll n;
  cin >> n;
  vll v = {-1, -1, -1};
  ll ans = 1;
  rep(i, n) {
    ll a;
    cin >> a;
    a--;
    ll cnt =
        upper_bound(v.begin(), v.end(), a) - lower_bound(v.begin(), v.end(), a);
    ans *= cnt;
    ans %= MOD;
    sort(v.begin(), v.end());
    if (lower_bound(v.begin(), v.end(), a) == v.end()) {
      cout << 0 << endl;
      return 0;
    }
    *lower_bound(v.begin(), v.end(), a) = a + 1;
    sort(v.begin(), v.end());
  }
  cout << ans << endl;
}
Problem p02845 (C++)

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

#define MOD (int)(1e9 + 7)

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

  vector<int> B(3);

  long long ans = 1;
  for (int i = 0; i < N; i++) {
    int index = -1;
    int cnt = 0;
    for (int j = 0; j < 3; j++) {
      if (B[j] == A[i]) {
        cnt++;
        index = j;
      }
    }

    ans *= cnt;
    ans %= MOD;

    B[index]++;
  }

  cout << ans << endl;

  return 0;
}

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

#define MOD (int)(1e9 + 7)

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

  vector<int> B(3);

  long long ans = 1;
  for (int i = 0; i < N; i++) {
    int index = -1;
    int cnt = 0;
    for (int j = 0; j < 3; j++) {
      if (B[j] == A[i]) {
        cnt++;
        index = j;
      }
    }

    ans *= cnt;
    ans %= MOD;

    if (index == -1)
      break;

    B[index]++;
  }

  cout << ans << endl;

  return 0;
}
Problem p02845 (C++)

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

using namespace std;

#define rep(i, n) for (int64_t i = 0; i < (n); i++)
#define irep(i, n) for (int64_t i = 0; i <= (n); i++)
#define rrep(i, n) for (int64_t i = (n)-1; i >= 0; i--)
#define rirep(i, n) for (int64_t i = n; i >= 0; i--)

const int64_t MOD = 1e9 + 7;

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

  vector<int> a(n);
  rep(i, n) { cin >> a[i]; }

  vector<int> cnt(3, 0);
  int64_t result = 1;
  rep(i, n) {
    result *= upper_bound(cnt.begin(), cnt.end(), a[i]) -
              lower_bound(cnt.begin(), cnt.end(), a[i]);
    result %= MOD;

    *(upper_bound(cnt.begin(), cnt.end(), a[i]) - 1) += 1;
  }
  cout << result << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

#define rep(i, n) for (int64_t i = 0; i < (n); i++)
#define irep(i, n) for (int64_t i = 0; i <= (n); i++)
#define rrep(i, n) for (int64_t i = (n)-1; i >= 0; i--)
#define rirep(i, n) for (int64_t i = n; i >= 0; i--)

const int64_t MOD = 1e9 + 7;

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

  vector<int> a(n);
  rep(i, n) { cin >> a[i]; }

  vector<int> cnt(3, 0);
  int64_t result = 1;
  rep(i, n) {
    int k = upper_bound(cnt.begin(), cnt.end(), a[i]) -
            lower_bound(cnt.begin(), cnt.end(), a[i]);
    if (k == 0) {
      cout << 0 << endl;
      return 0;
    }
    result *= k;
    result %= MOD;

    *(upper_bound(cnt.begin(), cnt.end(), a[i]) - 1) += 1;
  }
  cout << result << endl;

  return 0;
}
Problem p02845 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
using namespace std;
#define LL long long
#define LD long double
multiset<int> s;
const int mod = 1e9 + 7;
int n, a[100005];
LL ans = 1;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= 3; i++)
    s.insert(0);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    ans = ans * s.count(a[i]) % mod;
    s.erase(s.find(a[i]));
    s.insert(a[i] + 1);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
using namespace std;
#define LL long long
#define LD long double
multiset<int> s;
const int mod = 1e9 + 7;
int n, a[100005];
LL ans = 1;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= 3; i++)
    s.insert(0);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    ans = ans * s.count(a[i]) % mod;
    if (s.find(a[i]) != s.end()) {
      s.erase(s.find(a[i]));
      s.insert(a[i] + 1);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02845 (C++)

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

typedef long long int ll;
ll MOD = 1000000007;
ll INFL = 1ll << 60;
ll INF = 1 << 28;

template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  cout << "{";
  for (int i = 0; i < vec.size(); i++)
    os << vec[i] << (i + 1 == vec.size() ? "" : " ");
  cout << "}";
  return os;
}
// ====================================================================

int main() {
  ll n;
  cin >> n;
  if (n <= 2)
    return -1;
  vector<ll> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }

  vector<ll> dp(n);       // n人目まで考えた時…
  vector<ll> mae(100010); // 前にi人いるよって言った人の数
  dp[0] = 1;
  for (int i = 0; i < n; i++) {
    if (0 < i)
      dp[i] = dp[i - 1];
    if (v[i] == 0) {
      dp[i] = dp[i] * (3 - mae[0]) % MOD;
    } else {
      dp[i] = dp[i] * (mae[v[i] - 1] - mae[v[i]]) % MOD;
    }
    mae[v[i]]++;
  }

  cout << dp[n - 1] % MOD << endl;
}


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

typedef long long int ll;
ll MOD = 1000000007;
ll INFL = 1ll << 60;
ll INF = 1 << 28;

template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  cout << "{";
  for (int i = 0; i < vec.size(); i++)
    os << vec[i] << (i + 1 == vec.size() ? "" : " ");
  cout << "}";
  return os;
}
// ====================================================================

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

  vector<ll> dp(n);       // n人目まで考えた時…
  vector<ll> mae(100010); // 前にi人いるよって言った人の数
  dp[0] = 1;
  for (int i = 0; i < n; i++) {
    if (0 < i)
      dp[i] = dp[i - 1];
    if (v[i] == 0) {
      dp[i] = dp[i] * (3 - mae[0]) % MOD;
    } else {
      dp[i] = dp[i] * (mae[v[i] - 1] - mae[v[i]]) % MOD;
    }
    mae[v[i]]++;
  }

  cout << dp[n - 1] % MOD << endl;
}