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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  string s;
  cin >> s;
  int ans = 0;
  for (char i = '0'; i <= '9'; i++) {
    for (char j = '0'; j <= '9'; j++) {
      for (char k = '0'; k <= '9'; k++) {
        for (int a = 0; a < N - 2; a++) {
          if (s.at(a) == i) {
            for (int b = a + 1; a < N - 1; b++) {
              if (s.at(b) == j) {
                for (int c = b + 1; c < N; c++) {
                  if (s.at(c) == k) {
                    ans++;
                    break;
                  }
                }
                break;
              }
            }
            break;
          }
        }
      }
    }
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  string s;
  cin >> s;
  int ans = 0;
  for (char i = '0'; i <= '9'; i++) {
    for (char j = '0'; j <= '9'; j++) {
      for (char k = '0'; k <= '9'; k++) {
        for (int a = 0; a < N - 2; a++) {
          if (s.at(a) == i) {
            for (int b = a + 1; b < N - 1; b++) {
              if (s.at(b) == j) {
                for (int c = b + 1; c < N; c++) {
                  if (s.at(c) == k) {
                    ans++;
                    break;
                  }
                }
                break;
              }
            }
            break;
          }
        }
      }
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p02844 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int cnt = 0;
string s;
void f(int i) {
  int first = i / 100;
  int second = (i - first * 100) / 10;
  int third = i % 10;
  for (int a = 0; a + 2 < s.size(); a++) {
    if (s[a] - '0' == first) {
      for (int b = a + 1; b + 1 < s.size(); b++) {
        if (s[b] - '0' == second) {
          for (int c = b + 1; c < s.size(); c++)
            if (s[c] - '0' == third) {
              cnt++;
              return;
            }
        }
      }
    }
  }
}
int main() {
  int N;
  cin >> N >> s;
  for (int i = 0; i <= 999; i++)
    f(i);
  cout << cnt;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int cnt = 0;
string s;
void f(int i) {
  int first = i / 100;
  int second = (i - first * 100) / 10;
  int third = i % 10;
  int k = 0;
  int c[3] = {first, second, third};
  for (int a = 0; a < s.size(); a++) {
    if (s[a] - '0' == c[k])
      k++;
    if (k == 3) {
      cnt++;
      return;
    }
  }
}
int main() {
  int N;
  cin >> N >> s;
  for (int i = 0; i <= 999; i++)
    f(i);
  cout << cnt;
}
Problem p02844 (C++)

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

int main() {
  ll n;
  std::cin >> n;
  string s;
  std::cin >> s;
  vector<vector<vector<bool>>> done(10,
                                    vector<vector<bool>>(10, vector<bool>(10)));
  ll res = 0;

  vector<ll> dp1(n, 0);
  vector<vector<ll>> dp2(10, vector<ll>(10, 0));

  for (int i = 0; i < n; i++) {
    int x = s[i] - '0';
    for (int j = 0; j < 10; j++) {
      for (int k = 0; k < 10; k++) {
        if (done[j][k][x] == 1) {
          continue;
        }
        if (dp2[j][k]) {
          res++;
          done[j][k][x] = 1;
        }
      }
      if (dp1[j] == 1) {
        dp2[j][x] = 1;
      }
    }
    dp1[x] = 1;
  }
  std::cout << res << std::endl;
}


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

int main() {
  ll n;
  std::cin >> n;
  string s;
  std::cin >> s;
  vector<vector<vector<bool>>> done(10,
                                    vector<vector<bool>>(10, vector<bool>(10)));
  ll res = 0;

  vector<ll> dp1(10, 0);
  vector<vector<ll>> dp2(10, vector<ll>(10, 0));

  for (int i = 0; i < n; i++) {
    int x = s[i] - '0';
    for (int j = 0; j < 10; j++) {
      for (int k = 0; k < 10; k++) {
        if (done[j][k][x] == 1) {
          continue;
        }
        if (dp2[j][k]) {
          res++;
          done[j][k][x] = 1;
        }
      }
      if (dp1[j] == 1) {
        dp2[j][x] = 1;
      }
    }
    dp1[x] = 1;
  }
  std::cout << res << std::endl;
}
Problem p02844 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = int(a); i < int(b); ++i)
#define SIZE 10005
#define INF 1000000005LL
#define MOD 1000000007

using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

int n;
string s;

int a[12];
int c[SIZE][12];
int b[11][11][11];

int main() {
  cin >> n >> s;
  rep(i, 0, 12) c[n - 1][i] = -1;
  for (int i = n - 2; i >= 0; i--) {
    rep(j, 0, 10) {
      c[i][j] = c[i + 1][j];
      if ((s[i + 1] - '0') == j)
        c[i][j] = i + 1;
    }
  }

  /*
  rep(i,0,n){
    rep(j,0,5){
      cout << c[i][j] ;
    }
    cout << endl;
  }
  */

  rep(i, 0, n) {
    int A = s[i] - '0';
    rep(j, 0, 10) rep(k, 0, 10) {
      if (c[i][j] != -1) {
        if (c[c[i][j]][k] != -1)
          b[A][j][k] = 1;
      }
    }
  }

  int ans = 0;
  rep(i, 0, 10) rep(j, 0, 10) rep(k, 0, 10) { ans += b[i][j][k]; }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = int(a); i < int(b); ++i)
#define SIZE 30005
#define INF 1000000005LL
#define MOD 1000000007

using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

int n;
string s;

int a[12];
int c[SIZE][12];
int b[11][11][11];

int main() {
  cin >> n >> s;
  rep(i, 0, 12) c[n - 1][i] = -1;
  for (int i = n - 2; i >= 0; i--) {
    rep(j, 0, 10) {
      c[i][j] = c[i + 1][j];
      if ((s[i + 1] - '0') == j)
        c[i][j] = i + 1;
    }
  }

  /*
  rep(i,0,n){
    rep(j,0,5){
      cout << c[i][j] ;
    }
    cout << endl;
  }
  */

  rep(i, 0, n) {
    int A = s[i] - '0';
    rep(j, 0, 10) rep(k, 0, 10) {
      if (c[i][j] != -1) {
        if (c[c[i][j]][k] != -1)
          b[A][j][k] = 1;
      }
    }
  }

  int ans = 0;
  rep(i, 0, 10) rep(j, 0, 10) rep(k, 0, 10) { ans += b[i][j][k]; }

  cout << ans << endl;

  return 0;
}
Problem p02844 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define all(A) A.begin(), A.end()
typedef long long ll;

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  bool dp[30001][4][1000] = {};
  dp[0][0][0] = 1;
  rep(i, n) {
    rep(j, 4) {
      rep(k, 1000) {
        if (dp[i][j][k]) {
          dp[i + 1][j][k] = 1;
          dp[i + 1][j + 1][10 * k + s[i] - '0'] = (j < 3);
        }
      }
    }
  }
  int ans = 0;
  rep(i, 1000) if (dp[n][3][i]) ans++;
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define all(A) A.begin(), A.end()
typedef long long ll;

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  bool dp[30010][4][1000] = {};
  dp[0][0][0] = 1;
  rep(i, n) {
    rep(j, 4) {
      rep(k, 1000) {
        if (dp[i][j][k]) {
          dp[i + 1][j][k] = 1;
          dp[i + 1][j + 1][10 * k + s[i] - '0'] = (j < 3);
        }
      }
    }
  }
  int ans = 0;
  rep(i, 1000) if (dp[n][3][i]) ans++;
  cout << ans << endl;
}
Problem p02844 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define all(A) A.begin(), A.end()
typedef long long ll;

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  bool dp[30002][5][1000] = {};
  dp[0][0][0] = 1;
  rep(i, n) {
    rep(j, 4) {
      rep(k, 1000) {
        if (dp[i][j][k]) {
          dp[i + 1][j][k] = 1;
          dp[i + 1][j + 1][10 * k + s[i] - '0'] = (j < 3);
        }
      }
    }
  }
  int ans = 0;
  rep(i, 1000) if (dp[n][3][i]) ans++;
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define all(A) A.begin(), A.end()
typedef long long ll;

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  bool dp[30010][4][1000] = {};
  dp[0][0][0] = 1;
  rep(i, n) {
    rep(j, 4) {
      rep(k, 1000) {
        if (dp[i][j][k]) {
          dp[i + 1][j][k] = 1;
          dp[i + 1][j + 1][10 * k + s[i] - '0'] = (j < 3);
        }
      }
    }
  }
  int ans = 0;
  rep(i, 1000) if (dp[n][3][i]) ans++;
  cout << ans << endl;
}
Problem p02844 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, n) for (int i = 0; i < n; i++)
#define UNIQUE(v)                                                              \
  v.erase(unique(v.begin(), v.end()), v.end()) // sortしてから使う
#define INF 2147483647
#define LLINF 9223372036854775807LL

int main() {
  int n;
  string s;
  cin >> n >> s;
  ll count = 0;
  rep(i, 10) rep(j, 10) rep(k, 10) {
    string pin = to_string(i) + to_string(j) + to_string(k);
    int p1, p2, p3;
    bool ok1 = false;
    rep(l, n) {
      if (pin[0] == s[l]) {
        p1 = l;
        ok1 = true;
        break;
      }
    }
    bool ok2 = false;
    for (int l = p1 + 1; l < n; l++) {
      if (pin[1] == s[l]) {
        p2 = l;
        ok2 = true;
        break;
      }
    }
    bool ok3 = false;
    for (int l = p2 + 1; l < n; l++) {
      if (pin[2] == s[l]) {
        ok3 = true;
        break;
      }
    }
    if (ok1 == true && ok2 == true && ok3 == true)
      count++;
  }
  cout << count << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, n) for (int i = 0; i < n; i++)
#define UNIQUE(v)                                                              \
  v.erase(unique(v.begin(), v.end()), v.end()) // sortしてから使う
#define INF 2147483647
#define LLINF 9223372036854775807LL

int main() {
  int n;
  string s;
  cin >> n >> s;
  ll count = 0;
  rep(i, 10) rep(j, 10) rep(k, 10) {
    string pin = to_string(i) + to_string(j) + to_string(k);
    int p1 = 0;
    int p2 = 0;
    ;
    bool ok1 = false;
    rep(l, n) {
      if (pin[0] == s[l]) {
        p1 = l;
        ok1 = true;
        break;
      }
    }
    bool ok2 = false;
    for (int l = p1 + 1; l < n; l++) {
      if (pin[1] == s[l]) {
        p2 = l;
        ok2 = true;
        break;
      }
    }
    bool ok3 = false;
    for (int l = p2 + 1; l < n; l++) {
      if (pin[2] == s[l]) {
        ok3 = true;
        break;
      }
    }
    if (ok1 == true && ok2 == true && ok3 == true)
      count++;
  }
  cout << count << endl;
}
Problem p02844 (C++)

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

int main() {
  vector<int> key(1000, 0);
  int n;
  string s;
  cin >> n;
  cin >> s;
  vector<int> si(n);
  {
    for (int i = 0; i < n; ++i) {
      char temp = s[i];
      si[i] = atoi(&temp);
    }
  }

  int ret = 0;
  int imax = n - 3;
  int jmax = n - 2;
  int kmax = n - 1;
  int num = 0;
  vector<int> restoreI(10, 0);
  for (int i = 0; i <= imax; ++i) {
    if (restoreI[si[i]] == 1) {
      continue;
    }
    restoreI[si[i]] = 1;
    vector<int> restoreJ(10, 0);
    for (int j = i + 1; j <= jmax; ++j) {
      if (restoreJ[si[j]] == 1) {
        continue;
      }
      restoreJ[si[j]] = 1;
      vector<int> restoreK(10, 0);
      for (int k = j + 1; k <= kmax; ++k) {
        if (restoreK[si[k]] == 1) {
          continue;
        }
        restoreK[si[k]] = 1;

        num = si[i] * 100 + si[j] * 10 + si[k];
        if (key[num] == 0) {
          key[num] = 1;
          ++ret;
        }
      }
    }
  }
  cout << ret << endl;
  return 0;
}

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

int main() {
  vector<int> key(1000, 0);
  int n;
  string s;
  cin >> n;
  cin >> s;
  vector<int> si(n);
  {
    for (int i = 0; i < n; ++i) {
      if (s[i] == '0') {
        si[i] = 0;
      } else if (s[i] == '1') {
        si[i] = 1;
      } else if (s[i] == '2') {
        si[i] = 2;
      } else if (s[i] == '3') {
        si[i] = 3;
      } else if (s[i] == '4') {
        si[i] = 4;
      } else if (s[i] == '5') {
        si[i] = 5;
      } else if (s[i] == '6') {
        si[i] = 6;
      } else if (s[i] == '7') {
        si[i] = 7;
      } else if (s[i] == '8') {
        si[i] = 8;
      } else if (s[i] == '9') {
        si[i] = 9;
      }
    }
  }

  int ret = 0;
  int imax = n - 3;
  int jmax = n - 2;
  int kmax = n - 1;
  int num = 0;
  vector<int> restoreI(10, 0);
  for (int i = 0; i <= imax; ++i) {
    if (restoreI[si[i]] == 1) {
      continue;
    }
    restoreI[si[i]] = 1;
    vector<int> restoreJ(10, 0);
    for (int j = i + 1; j <= jmax; ++j) {
      if (restoreJ[si[j]] == 1) {
        continue;
      }
      restoreJ[si[j]] = 1;
      vector<int> restoreK(10, 0);
      for (int k = j + 1; k <= kmax; ++k) {
        if (restoreK[si[k]] == 1) {
          continue;
        }
        restoreK[si[k]] = 1;

        num = si[i] * 100 + si[j] * 10 + si[k];
        if (key[num] == 0) {
          key[num] = 1;
          ++ret;
        }
      }
    }
  }
  cout << ret << endl;
  return 0;
}
Problem p02844 (C++)

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

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

#define mod 1000000007
#define inf 1000000000
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define popcount(x) __builtin_popcountll(x)

const long double PI = acos(-1);

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

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

  ll ans = 0;
  map<string, ll> m;
  for (ll i = 0; i < n - 2; i++) {
    for (ll j = i + 1; j < n - 1; j++) {
      for (ll k = j + 1; k < n; k++) {
        string t = "";
        t = t + s[i] + s[j] + s[k];
        m[t]++;
        if (m[t] == 1)
          ans++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

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

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

#define mod 1000000007
#define inf 1000000000
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define popcount(x) __builtin_popcountll(x)

const long double PI = acos(-1);

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

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

  ll ans = 0;
  for (ll i = 0; i < 1000; i++) {
    string t = "";
    t += i / 100 + '0', t += (i / 10) % 10 + '0', t += i % 10 + '0';
    // cout << t << endl;
    ll cnt = 0;
    for (ll j = 0; j < n; j++) {
      if (s[j] == t[cnt])
        cnt++;
      if (cnt == 3) {
        ans++;
        break;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02844 (Python)

Buggy submission (status: Time Limit Exceeded):
n = int(input())
s = input()
pattern = [tuple(s[:3])]
for c in s[3:]:
    for i in range(len(pattern)):
        p1, p2, p3 = pattern[i]
        if (p1, p2, c) not in pattern:
            pattern.append((p1, p2, c))
        if (p1, p3, c) not in pattern:
            pattern.append((p1, p3, c))
        if (p2, p3, c) not in pattern:
            pattern.append((p2, p3, c))
print(len(pattern))


Fixed submission:
n = int(input())
s = input()
cnt = 0

for i in range(1000):
    p = str(i).zfill(3)
    p1 = s.find(p[0])
    if p1 == -1:
        continue
    p2 = s.find(p[1], p1 + 1)
    if p2 == -1:
        continue
    p3 = s.find(p[2], p2 + 1)
    if p3 == -1:
        continue
    cnt += 1
print(cnt)
Problem p02844 (C++)

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

using namespace std;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;

void SMTB2019D() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  vi d1(10, -1);
  for (int k = 0; k < N; ++k) {
    int i = S[k] - '0';
    const char x = S[k];
    int j = atoi(&x);

    if (j < 0 || j > 10)
      throw "Bad";
    if (d1[i] < 0)
      d1[i] = k;
  }
  vi d2(100, -1);
  for (int k = 1; k < N; ++k) {
    int i = S[k] - '0';
    char x = S[k];
    int j = atoi(&x);
    //		if (i != j)
    //			throw "Bad";
    for (int j = 0; j < 10; ++j) {
      if (d1[j] >= 0 && d1[j] < k && d2[j * 10 + i] < 0)
        d2[j * 10 + i] = k;
    }
  }
  vi d3(1000, -1);
  for (int k = 2; k < N; ++k) {
    int i = S[k] - '0';
    char x = S[k];
    int j = atoi(&x);
    //		if (i != j)
    //			throw "Bad";
    for (int j = 0; j < 100; ++j) {
      if (d2[j] >= 0 && d2[j] < k && d3[j * 10 + i] < 0)
        d3[j * 10 + i] = k;
    }
  }
  int ct = 0;
  for (int i = 0; i < 1000; ++i) {
    if (d3[i] > 0) {
      ++ct;
    }
  }
  cout << ct << endl;
}
int main() {
  SMTB2019D();
  return 0;
}

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

using namespace std;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;

void SMTB2019D() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  vi d1(10, -1);
  for (int k = 0; k < N; ++k) {
    int i = S[k] - '0';
    const char x = S[k];
    int j = atoi(&x);

    if (x - '0' != i)
      throw "Bad";
    if (d1[i] < 0)
      d1[i] = k;
  }
  vi d2(100, -1);
  for (int k = 1; k < N; ++k) {
    int i = S[k] - '0';
    char x = S[k];
    int j = atoi(&x);
    //		if (i != j)
    //			throw "Bad";
    for (int j = 0; j < 10; ++j) {
      if (d1[j] >= 0 && d1[j] < k && d2[j * 10 + i] < 0)
        d2[j * 10 + i] = k;
    }
  }
  vi d3(1000, -1);
  for (int k = 2; k < N; ++k) {
    int i = S[k] - '0';
    char x = S[k];
    int j = atoi(&x);
    //		if (i != j)
    //			throw "Bad";
    for (int j = 0; j < 100; ++j) {
      if (d2[j] >= 0 && d2[j] < k && d3[j * 10 + i] < 0)
        d3[j * 10 + i] = k;
    }
  }
  int ct = 0;
  for (int i = 0; i < 1000; ++i) {
    if (d3[i] > 0) {
      ++ct;
    }
  }
  cout << ct << endl;
}
int main() {
  SMTB2019D();
  return 0;
}
Problem p02844 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;

int main() {
  int N, check;
  string S;
  int ans = 0;
  vector<int> p(100);
  cin >> N >> S;
  for (int i = 0; i < 1000; i++) {
    check = 0;
    p.at(2) = i % 10;
    p.at(1) = (i / 10) % 10;
    p.at(0) = i / 100;
    for (int j = 0; j < S.size(); j++) {
      if (p.at(check) == S.at(j) - '0')
        check++;
    }
    if (check == 3)
      ans++;
  }
  cout << ans;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;

int main() {
  int N, check;
  string S;
  int ans = 0;
  vector<int> p(100, 10);
  cin >> N >> S;
  for (int i = 0; i < 1000; i++) {
    check = 0;
    p.at(2) = i % 10;
    p.at(1) = (i / 10) % 10;
    p.at(0) = i / 100;
    for (int j = 0; j < S.size(); j++) {
      if (p.at(check) == S.at(j) - '0')
        check++;
    }
    if (check == 3)
      ans++;
  }
  cout << ans;
}
Problem p02844 (C++)

Buggy submission (status: Time Limit Exceeded):
#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;
string s, t;
int n, ans;
bool check() {
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == t[0]) {
      for (int j = i + 1; j < s.length(); j++) {
        if (s[j] == t[1]) {
          for (int k = j + 1; k < s.length(); k++) {
            if (s[k] == t[2]) {
              return true;
            }
          }
        }
      }
      return false;
    }
  }
  return false;
}
int main() {
  cin >> n >> s;
  t = "   ";
  for (char i = '0'; i <= '9'; i++) {
    for (char j = '0'; j <= '9'; j++) {
      for (char k = '0'; k <= '9'; k++) {
        t[0] = i, t[1] = j, t[2] = k;
        if (check()) {
          ans++;
        }
      }
    }
  }
  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;
string s, t;
int n, ans;
bool check() {
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == t[0]) {
      for (int j = i + 1; j < s.length(); j++) {
        if (s[j] == t[1]) {
          for (int k = j + 1; k < s.length(); k++) {
            if (s[k] == t[2]) {
              return true;
            }
          }
          return false;
        }
      }
      return false;
    }
  }
  return false;
}
int main() {
  cin >> n >> s;
  t = "   ";
  for (char i = '0'; i <= '9'; i++) {
    for (char j = '0'; j <= '9'; j++) {
      for (char k = '0'; k <= '9'; k++) {
        t[0] = i, t[1] = j, t[2] = k;
        if (check()) {
          ans++;
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02844 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
int n, m;
std::map<std::string, int> cnt;
std::string s;
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cin >> n;
  std::cin >> s;
  std::vector<int> max_pos(n, -1);
  std::vector<int> min_pos(n, n + 1);
  for (int i = 0; i <= 9; ++i)
    min_pos[i] = n + 1, max_pos[i] = -1;
  for (int i = 0; i < n; ++i) {
    int m = s[i] - '0';
    max_pos[m] = i;
    min_pos[m] = std::min(min_pos[m], i);
  }
  std::map<std::string, int> memo;
  for (int i = 0; i < n; ++i) {
    int key = s[i] - '0';
    for (int l = 0; l <= 9; ++l) {
      for (int r = 0; r <= 9; ++r) {
        if (max_pos[r] == -1 or min_pos[l] == n + 1)
          continue;
        if (min_pos[l] < i && i < max_pos[r])
          memo[std::to_string(l) + std::to_string(key) + std::to_string(r)] +=
              1;
      }
    }
  }
  std::cout << memo.size() << '\n';
}


Fixed submission:
#include <bits/stdc++.h>
int n, m;
std::map<std::string, int> cnt;
std::string s;
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cin >> n;
  std::cin >> s;
  std::vector<int> max_pos(10, -1);
  std::vector<int> min_pos(10, n + 1);
  for (int i = 0; i <= 9; ++i)
    min_pos[i] = n + 1, max_pos[i] = -1;
  for (int i = 0; i < n; ++i) {
    int m = s[i] - '0';
    max_pos[m] = i;
    min_pos[m] = std::min(min_pos[m], i);
  }
  std::map<std::string, int> memo;
  for (int i = 0; i < n; ++i) {
    int key = s[i] - '0';
    for (int l = 0; l <= 9; ++l) {
      for (int r = 0; r <= 9; ++r) {
        if (max_pos[r] == -1 or min_pos[l] == n + 1)
          continue;
        if (min_pos[l] < i && i < max_pos[r])
          memo[std::to_string(l) + std::to_string(key) + std::to_string(r)] +=
              1;
      }
    }
  }
  std::cout << memo.size() << '\n';
}
Problem p02844 (C++)

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

int N;
string S;

int dp[30000][11][11][11];
bool used[10][10][10];
int ans;

void dfs(int n, int a, int b, int c) {
  if (dp[n][a][b][c])
    return;
  dp[n][a][b][c] = 1;
  if (n == N || c < 10) {
    if (a < 10 && b < 10 && c < 10) {
      if (!used[a][b][c])
        ans++;
      used[a][b][c] = true;
    }
    return;
  }

  dfs(n + 1, a, b, c);
  if (a == 10) {
    dfs(n + 1, S[n] - '0', b, c);
  } else if (b == 10) {
    dfs(n + 1, a, S[n] - '0', c);
  } else if (c == 10) {
    dfs(n + 1, a, b, S[n] - '0');
  }

  return;
}

struct St {
  int n, a, b, c;
  St(int n, int a, int b, int c) : n(n), a(a), b(b), c(c){};
};

void solve() {
  stack<St> st;
  st.push(St(0, 10, 10, 10));
  while (st.size()) {
    St q = st.top();
    st.pop();
    int n = q.n, a = q.a, b = q.b, c = q.c;
    if (dp[n][a][b][c])
      continue;
    dp[n][a][b][c] = 1;
    if (n == N || c < 10) {
      if (a < 10 && b < 10 && c < 10) {
        if (!used[a][b][c])
          ans++;
        used[a][b][c] = true;
      }
      continue;
    }

    st.push(St(n + 1, a, b, c));
    if (a == 10) {
      st.push(St(n + 1, S[n] - '0', b, c));
    } else if (b == 10) {
      st.push(St(n + 1, a, S[n] - '0', c));
    } else if (c == 10) {
      st.push(St(n + 1, a, b, S[n] - '0'));
    }
  }
}

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  cin >> N;
  cin >> S;

  // dfs(0, 10, 10, 10);
  solve();

  cout << ans << endl;

  return 0;
}


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

int N;
string S;

int dp[30010][11][11][11];
bool used[10][10][10];
int ans;

void dfs(int n, int a, int b, int c) {
  if (dp[n][a][b][c])
    return;
  dp[n][a][b][c] = 1;
  if (n == N || c < 10) {
    if (a < 10 && b < 10 && c < 10) {
      if (!used[a][b][c])
        ans++;
      used[a][b][c] = true;
    }
    return;
  }

  dfs(n + 1, a, b, c);
  if (a == 10) {
    dfs(n + 1, S[n] - '0', b, c);
  } else if (b == 10) {
    dfs(n + 1, a, S[n] - '0', c);
  } else if (c == 10) {
    dfs(n + 1, a, b, S[n] - '0');
  }

  return;
}

struct St {
  int n, a, b, c;
  St(int n, int a, int b, int c) : n(n), a(a), b(b), c(c){};
};

void solve() {
  stack<St> st;
  st.push(St(0, 10, 10, 10));
  while (st.size()) {
    St q = st.top();
    st.pop();
    int n = q.n, a = q.a, b = q.b, c = q.c;
    if (dp[n][a][b][c])
      continue;
    dp[n][a][b][c] = 1;
    if (n == N || c < 10) {
      if (a < 10 && b < 10 && c < 10) {
        if (!used[a][b][c])
          ans++;
        used[a][b][c] = true;
      }
      continue;
    }

    st.push(St(n + 1, a, b, c));
    if (a == 10) {
      st.push(St(n + 1, S[n] - '0', b, c));
    } else if (b == 10) {
      st.push(St(n + 1, a, S[n] - '0', c));
    } else if (c == 10) {
      st.push(St(n + 1, a, b, S[n] - '0'));
    }
  }
}

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  cin >> N;
  cin >> S;

  // dfs(0, 10, 10, 10);
  solve();

  cout << ans << endl;

  return 0;
}
Problem p02844 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)
#define ll long long
#define mp make_pair
#define pb push_back
#define debug(...) fprintf(stderr, __VA_ARGS__)
using namespace std;
template <typename T> void ckmax(T &a, T b) {
  if (b > a)
    a = b;
}
template <typename T> void ckmin(T &a, T b) {
  if (b < a)
    a = b;
}

int f[10][10][10], g[10][10], h[10], n, a[20000 + 5];
char tmp[30000 + 5];

int main() {
  scanf("%d", &n);
  scanf("%s", tmp + 1);
  rep(i, 1, n) {
    a[i] = tmp[i] - '0';
    rep(x, 0, 9) rep(y, 0, 9) f[x][y][a[i]] |= g[x][y];
    rep(x, 0, 9) g[x][a[i]] |= h[x];
    h[a[i]] = 1;
  }
  int ans = 0;
  rep(x, 0, 9) rep(y, 0, 9) rep(z, 0, 9) ans += f[x][y][z];
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)
#define ll long long
#define mp make_pair
#define pb push_back
#define debug(...) fprintf(stderr, __VA_ARGS__)
using namespace std;
template <typename T> void ckmax(T &a, T b) {
  if (b > a)
    a = b;
}
template <typename T> void ckmin(T &a, T b) {
  if (b < a)
    a = b;
}

int f[10][10][10], g[10][10], h[10], n, a[30000 + 5];
char tmp[30000 + 5];

int main() {
  scanf("%d", &n);
  scanf("%s", tmp + 1);
  rep(i, 1, n) {
    a[i] = tmp[i] - '0';
    rep(x, 0, 9) rep(y, 0, 9) f[x][y][a[i]] |= g[x][y];
    rep(x, 0, 9) g[x][a[i]] |= h[x];
    h[a[i]] = 1;
  }
  int ans = 0;
  rep(x, 0, 9) rep(y, 0, 9) rep(z, 0, 9) ans += f[x][y][z];
  cout << ans << endl;
  return 0;
}
Problem p02844 (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")
#include <bits/stdc++.h>
#define int long long
using namespace std;
vector<vector<int>> s;
void find(vector<int> a, int k) {
  for (int i = 0; i < 10; i++) {
    vector<int> temp = a;
    temp.push_back(i);
    if (k < 4)
      find(temp, k + 1);
    else {
      s.push_back(a);
      break;
    }
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("INPUT.txt", "r", stdin);
  freopen("OUTPUT.txt", "w", stdout);
#endif

  // -------------------------------------Code starts
  // here---------------------------------------------------------------------

  vector<int> tt;
  find(tt, 1);
  int n;
  cin >> n;
  string str;
  cin >> str;
  int cnt = 0;
  for (auto i : s) {
    int ind = 0;
    for (int j = 0; j < n; j++) {
      if (i[ind] == str[j] - '0') {
        ind++;
        if (ind == 3)
          break;
      }
    }
    if (ind == 3) {
      cnt++;
    }
  }
  cout << cnt;
  // -------------------------------------Code ends
  // here------------------------------------------------------
  clock_t clk;
  clk = clock();

  clk = clock() - clk;
  cerr << fixed << setprecision(6) << "Time: " << ((double)clk) / CLOCKS_PER_SEC
       << "\n";
  return 0;
}

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")
#include <bits/stdc++.h>
#define int long long
using namespace std;
vector<vector<int>> s;
void find(vector<int> a, int k) {
  for (int i = 0; i < 10; i++) {
    vector<int> temp = a;
    temp.push_back(i);
    if (k < 4)
      find(temp, k + 1);
    else {
      s.push_back(a);
      break;
    }
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  //  #ifndef ONLINE_JUDGE
  //     freopen ("INPUT.txt" , "r" , stdin);
  //     freopen ("OUTPUT.txt" , "w" , stdout);
  //  #endif

  // -------------------------------------Code starts
  // here---------------------------------------------------------------------

  vector<int> tt;
  find(tt, 1);
  int n;
  cin >> n;
  string str;
  cin >> str;
  int cnt = 0;
  for (auto i : s) {
    int ind = 0;
    for (int j = 0; j < n; j++) {
      if (i[ind] == str[j] - '0') {
        ind++;
        if (ind == 3)
          break;
      }
    }
    if (ind == 3) {
      cnt++;
    }
  }
  cout << cnt;
  // -------------------------------------Code ends
  // here------------------------------------------------------
  clock_t clk;
  clk = clock();

  clk = clock() - clk;
  cerr << fixed << setprecision(6) << "Time: " << ((double)clk) / CLOCKS_PER_SEC
       << "\n";
  return 0;
}
Problem p02845 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iomanip>
using namespace std;
#define reps(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) reps(i, 0, n)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
#define mp make_pair
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N, M, H, W, K, Q, A, B;
string S;
// const ll MOD = 998244353;
const ll MOD = (1e+9) + 7;
const ll INF = 1LL << 60;
typedef pair<ll, ll> P;

int main() {
  cin >> N;
  vec num(3, 0);
  ll ans = 1;
  rep(i, N) {
    cin >> A;
    ll temp = 0, id = -1;
    rep(j, 3) {
      if (num[j] == A) {
        ++temp;
        id = (id == -1 ? j : id);
      }
    }
    (ans *= temp) %= MOD;
    ++num[id];
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <iomanip>
using namespace std;
#define reps(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) reps(i, 0, n)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
#define mp make_pair
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N, M, H, W, K, Q, A, B;
string S;
// const ll MOD = 998244353;
const ll MOD = (1e+9) + 7;
const ll INF = 1LL << 60;
typedef pair<ll, ll> P;

int main() {
  cin >> N;
  vec num(3, 0);
  ll ans = 1;
  rep(i, N) {
    cin >> A;
    ll temp = 0, id = -1;
    rep(j, 3) {
      if (num[j] == A) {
        ++temp;
        id = (id == -1 ? j : id);
      }
    }
    if (id != -1) {
      (ans *= temp) %= MOD;
      ++num[id];
    } else {
      ans = 0;
      break;
    }
  }
  cout << ans << endl;
}
Problem p02845 (C++)

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

#define vi vector<int>
#define vl vector<long long>
#define vvi vector<vector<int>>
#define vvl vector<vector<ll>>
#define vp vector<P>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REPD(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define FORD(i, a, b) for (int i = (int)(a); i >= (int)(b); i--)
#define INF 1e18
#define INFTY 1e9
const int mod = 1000000007;
#define SIZE_OF_ARRAY(array) (sizeof(array) / sizeof(array[0]))

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

// cout << fixed << setprecision(10) << y << endl;
struct edge {
  int to, cost;
};
vector<vector<edge>> es;
const int MAX_N = 10010;

int gcd(int a, int b) {
  if (b > a) {
    int c = b;
    b = a;
    a = c;
  }
  int r = a % b;
  if (r == 0)
    return b;
  return gcd(b, r);
}

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

int main() {
  int n;
  cin >> n;
  vi a(n);
  REP(i, n) cin >> a[i];
  ll res = 1;
  vi c = {0, 0, 0};
  REP(i, n) {
    int cnt = 0;
    int ind = -1;
    REP(j, 3) {
      if (a[i] == c[j]) {
        cnt += 1;
        if (ind < 0)
          ind = j;
      }
    }
    res *= cnt;
    res %= mod;
    c[ind] += 1;
  }
  cout << res << endl;
  return 0;
}

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

#define vi vector<int>
#define vl vector<long long>
#define vvi vector<vector<int>>
#define vvl vector<vector<ll>>
#define vp vector<P>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REPD(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define FORD(i, a, b) for (int i = (int)(a); i >= (int)(b); i--)
#define INF 1e18
#define INFTY 1e9
const int mod = 1000000007;
#define SIZE_OF_ARRAY(array) (sizeof(array) / sizeof(array[0]))

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

// cout << fixed << setprecision(10) << y << endl;
struct edge {
  int to, cost;
};
vector<vector<edge>> es;
const int MAX_N = 10010;

int gcd(int a, int b) {
  if (b > a) {
    int c = b;
    b = a;
    a = c;
  }
  int r = a % b;
  if (r == 0)
    return b;
  return gcd(b, r);
}

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

int main() {
  int n;
  cin >> n;
  vi a(n);
  REP(i, n) cin >> a[i];
  ll res = 1;
  vi c = {0, 0, 0};
  REP(i, n) {
    int cnt = 0;
    int ind = -1;
    REP(j, 3) {
      if (a[i] == c[j]) {
        cnt += 1;
        if (ind < 0)
          ind = j;
      }
    }
    res *= cnt;
    res %= mod;
    if (ind < 0)
      res = 0;
    else
      c[ind] += 1;
  }
  cout << res << endl;
  return 0;
}
Problem p02845 (Python)

Buggy submission (status: Runtime Error):
MOD = 1000000007

n = int(input())
A = list(map(int, input().split()))

vec = [0, 0, 0]
num = 1

for a in A:
    idx = [i for i, x in enumerate(vec) if x == a]
    num = (num * len(idx)) % MOD
    vec[idx[0]] += 1

print(num)


Fixed submission:
MOD = 1000000007

n = int(input())
A = list(map(int, input().split()))

vec = [0, 0, 0]
num = 1

for a in A:
    idx = [i for i, x in enumerate(vec) if x == a]
    if len(idx) == 0:
        print(0)
        exit()
    num = (num * len(idx)) % MOD
    vec[idx[0]] += 1

print(num)
Problem p02845 (Python)

Buggy submission (status: Runtime Error):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10**9)
INF = 1 << 60


def main():
    MOD = 1000000007
    N, *A = map(int, read().split())
    C = [0] * N
    C[0] = 3
    ans = 1
    for a in A:
        ans = ans * C[a] % MOD
        if ans == 0:
            print(0)
            return
        C[a] -= 1
        C[a + 1] += 1

    print(ans)

    return


if __name__ == "__main__":
    main()


Fixed submission:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10**9)
INF = 1 << 60


def main():
    MOD = 1000000007
    N, *A = map(int, read().split())
    C = [0] * (N + 1)
    C[0] = 3
    ans = 1
    for a in A:
        ans = ans * C[a] % MOD
        if ans == 0:
            print(0)
            return
        C[a] -= 1
        C[a + 1] += 1

    print(ans)

    return


if __name__ == "__main__":
    main()
Problem p02845 (C++)

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

using namespace std;

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<string> vst;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;

const ll mod = (1e+9) + 7;
const ll inf = 1e+16;

#define rep(i, m, n) for (ll i = m; i < n; i++)
#define repr(i, m, n) for (ll i = m; i >= n; i--)

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

  ll n;
  cin >> n;
  vll a(n);
  rep(i, 0, n) cin >> a[i];

  vll cnt(35000, 0);

  vll dp(n + 1, 0);

  dp[0] = 1;

  rep(i, 0, n) {
    if (a[i] == 0)
      dp[i + 1] = (3 - cnt[0]) * dp[i];
    else if (cnt[a[i] - 1] - cnt[a[i]] == 1)
      dp[i + 1] = dp[i];
    else if (cnt[a[i] - 1] - cnt[a[i]] == 2)
      dp[i + 1] = 2 * dp[i];
    else if (cnt[a[i] - 1] - cnt[a[i]] == 3)
      dp[i + 1] = 3 * dp[i];
    dp[i + 1] %= mod;
    cnt[a[i]]++;
  }

  cout << dp[n] << endl;
}


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

using namespace std;

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<string> vst;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;

const ll mod = (1e+9) + 7;
const ll inf = 1e+16;

#define rep(i, m, n) for (ll i = m; i < n; i++)
#define repr(i, m, n) for (ll i = m; i >= n; i--)

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

  ll n;
  cin >> n;
  vll a(n);
  rep(i, 0, n) cin >> a[i];

  vll cnt(100005, 0);

  vll dp(n + 1, 0);

  dp[0] = 1;

  rep(i, 0, n) {
    if (a[i] == 0)
      dp[i + 1] = (3 - cnt[0]) * dp[i];
    else if (cnt[a[i] - 1] - cnt[a[i]] == 1)
      dp[i + 1] = dp[i];
    else if (cnt[a[i] - 1] - cnt[a[i]] == 2)
      dp[i + 1] = 2 * dp[i];
    else if (cnt[a[i] - 1] - cnt[a[i]] == 3)
      dp[i + 1] = 3 * dp[i];
    dp[i + 1] %= mod;
    cnt[a[i]]++;
  }

  cout << dp[n] << endl;
}
Problem p02845 (Python)

Buggy submission (status: Runtime Error):
#!/usr/bin/python3

import os
import sys


def main():
    N = read_int()
    A = read_ints()
    print(solve(N, A))


MOD = 1000000007


def solve(N, A):
    c = 1
    nums = [0, 0, 0]

    for a in A:
        z = nums.count(a)
        c *= z
        c %= MOD
        nums[nums.index(a)] = a + 1

    return c


###############################################################################

DEBUG = "DEBUG" in os.environ


def inp():
    return sys.stdin.readline().rstrip()


def read_int():
    return int(inp())


def read_ints():
    return [int(e) for e in inp().split()]


def dprint(*value, sep=" ", end="\n"):
    if DEBUG:
        print(*value, sep=sep, end=end)


if __name__ == "__main__":
    main()


Fixed submission:
#!/usr/bin/python3

import os
import sys


def main():
    N = read_int()
    A = read_ints()
    print(solve(N, A))


MOD = 1000000007


def solve(N, A):
    c = 1
    nums = [0, 0, 0]

    for a in A:
        z = nums.count(a)
        if z == 0:
            return 0
        c *= z
        c %= MOD
        nums[nums.index(a)] = a + 1

    return c


###############################################################################

DEBUG = "DEBUG" in os.environ


def inp():
    return sys.stdin.readline().rstrip()


def read_int():
    return int(inp())


def read_ints():
    return [int(e) for e in inp().split()]


def dprint(*value, sep=" ", end="\n"):
    if DEBUG:
        print(*value, sep=sep, end=end)


if __name__ == "__main__":
    main()
Problem p02845 (Python)

Buggy submission (status: Runtime Error):
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**7)

n, *a = map(int, read().split())
ans = 1
mod = 10**9 + 7
memo = [0, 0, 0]
for check in a:
    ans *= memo.count(check)
    ans %= mod
    memo[memo.index(check)] += 1
print(ans)


Fixed submission:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**7)

n, *a = map(int, read().split())
ans = 1
mod = 10**9 + 7
memo = [0, 0, 0]
for check in a:
    if check in memo:
        ans *= memo.count(check)
        ans %= mod
        memo[memo.index(check)] += 1
    else:
        print(0)
        exit()
print(ans)
Problem p02845 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

template <typename T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <typename T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}

const ll mod = 1000000007;

int main() {
  int n;
  cin >> n;
  VI a(n);
  REP(i, n) a[i] = in();
  ll ans = 1;
  VI cnt(3);
  REP(i, n) {
    VI c;
    REP(j, 3) {
      if (cnt[j] == a[i]) {
        c.push_back(j);
      }
    }
    ans = (ans * (ll)c.size()) % mod;
    cnt[c[0]]++;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

template <typename T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <typename T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}

const ll mod = 1000000007;

int main() {
  int n;
  cin >> n;
  VI a(n);
  REP(i, n) a[i] = in();
  ll ans = 1;
  VI cnt(3);
  REP(i, n) {
    VI c;
    REP(j, 3) {
      if (cnt[j] == a[i]) {
        c.push_back(j);
      }
    }
    if (c.empty()) {
      cout << 0 << endl;
      return 0;
    }
    ans = (ans * (ll)c.size()) % mod;
    cnt[c[0]]++;
  }
  cout << ans << endl;
  return 0;
}
Problem p02845 (C++)

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

const long long MOD = 1000000007;

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

  long long res = 1;
  vector<int> C(3);
  for (int i = 0; i < N; i++) {
    long long cnt = 0, id = -1;
    if (A[i] == C[0]) {
      cnt++;
      id = 0;
    }
    if (A[i] == C[1]) {
      cnt++;
      id = 1;
    }
    if (A[i] == C[2]) {
      cnt++;
      id = 2;
    }

    res *= cnt;
    res %= MOD;
    C[id]++;
  }

  cout << res << endl;
  return 0;
}

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

const long long MOD = 1000000007;

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

  long long res = 1;
  vector<int> C(3);
  for (int i = 0; i < N; i++) {
    long long cnt = 0, id = -1;
    if (A[i] == C[0]) {
      cnt++;
      id = 0;
    }
    if (A[i] == C[1]) {
      cnt++;
      id = 1;
    }
    if (A[i] == C[2]) {
      cnt++;
      id = 2;
    }

    if (id == -1) {
      cout << "0" << endl;
      return 0;
    }

    res *= cnt;
    res %= MOD;
    C[id]++;
  }

  cout << res << endl;
  return 0;
}
Problem p02845 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repo(i, n) for (int i = 1; i < (int)(n); i++)
#define pb push_back
#define mp make_pair
#define np next_permutation
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define mod 1000000007
#define pi acos(-1.0)
const ll INF = 1LL << 61;
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;
}
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
ll jou(ll N, ll P) {
  if (P == 0)
    return 1;
  if (P % 2 == 0) {
    ll t = jou(N, P / 2);
    return t * t % mod;
  }
  return (N * jou(N, P - 1)) % mod;
} // jou(n,mod-2)で逆元
// intの最大値2147483647 ≒ 2×10^9
// long longの最大値9223372036854775807 ≒ 9×10^18
//'大文字'+=32;    で小文字に
//   cout << fixed << setprecision (20);   小数点以下2０桁まで
// 実行時間制約2秒では２×10^8回くらいまで計算できる

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

  vector<ll> p(n);
  rep(i, n) { cin >> p[i]; }

  ll ans = 1;
  vector<ll> q = {0, 0, 0};
  rep(i, n) {
    int cnt = 0;
    int memo;
    rep(j, 3) {
      if (p[i] == q[j]) {
        cnt++;
        memo = j;
      }
    }
    ans *= cnt;
    ans %= mod;
    q[memo]++;
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repo(i, n) for (int i = 1; i < (int)(n); i++)
#define pb push_back
#define mp make_pair
#define np next_permutation
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define mod 1000000007
#define pi acos(-1.0)
const ll INF = 1LL << 61;
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;
}
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
ll jou(ll N, ll P) {
  if (P == 0)
    return 1;
  if (P % 2 == 0) {
    ll t = jou(N, P / 2);
    return t * t % mod;
  }
  return (N * jou(N, P - 1)) % mod;
} // jou(n,mod-2)で逆元
// intの最大値2147483647 ≒ 2×10^9
// long longの最大値9223372036854775807 ≒ 9×10^18
//'大文字'+=32;    で小文字に
//   cout << fixed << setprecision (20);   小数点以下2０桁まで
// 実行時間制約2秒では２×10^8回くらいまで計算できる

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

  vector<ll> p(n);
  rep(i, n) { cin >> p[i]; }

  ll ans = 1;
  vector<ll> q = {0, 0, 0};
  rep(i, n) {
    int cnt = 0;
    int memo;
    rep(j, 3) {
      if (p[i] == q[j]) {
        cnt++;
        memo = j;
      }
    }
    if (cnt == 0) {
      cout << 0 << endl;
      return 0;
    }
    ans *= cnt;
    ans %= mod;
    q[memo]++;
  }

  cout << ans << endl;
}