Problem p02845 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
// #define cerr if (false) cerr
#define db(x) cerr << #x << "=" << x << endl
#define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z)                                                           \
  cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \
       << endl
#define dbv(v)                                                                 \
  cerr << #v << "=";                                                           \
  for (auto _x : v)                                                            \
    cerr << _x << ", ";                                                        \
  cerr << endl
#define dba(a, n)                                                              \
  cerr << #a << "=";                                                           \
  for (int _i = 0; _i < (n); ++_i)                                             \
    cerr << a[_i] << ", ";                                                     \
  cerr << endl
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &x) {
  return os << "(" << x.first << "," << x.second << ")";
}
typedef long long ll;
typedef long double ld;
int main() {
  int n;
  scanf("%d", &n);
  vector<int> a(3);
  const int MOD = 1e9 + 7;
  ll ans = 1;
  for (int i = 0; i < n; ++i) {
    int x;
    scanf("%d", &x);
    int las, cnt = 0;
    for (int j = 0; j < 3; ++j) {
      if (a[j] == x) {
        ++cnt;
        las = j;
      }
    }
    assert(cnt > 0);
    ans = ans * cnt % MOD;
    ++a[las];
  }
  printf("%lld\n", ans);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
// #define cerr if (false) cerr
#define db(x) cerr << #x << "=" << x << endl
#define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z)                                                           \
  cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \
       << endl
#define dbv(v)                                                                 \
  cerr << #v << "=";                                                           \
  for (auto _x : v)                                                            \
    cerr << _x << ", ";                                                        \
  cerr << endl
#define dba(a, n)                                                              \
  cerr << #a << "=";                                                           \
  for (int _i = 0; _i < (n); ++_i)                                             \
    cerr << a[_i] << ", ";                                                     \
  cerr << endl
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &x) {
  return os << "(" << x.first << "," << x.second << ")";
}
typedef long long ll;
typedef long double ld;
int main() {
  int n;
  scanf("%d", &n);
  vector<int> a(3);
  const int MOD = 1e9 + 7;
  ll ans = 1;
  for (int i = 0; i < n; ++i) {
    int x;
    scanf("%d", &x);
    int las, cnt = 0;
    for (int j = 0; j < 3; ++j) {
      if (a[j] == x) {
        ++cnt;
        las = j;
      }
    }
    if (cnt == 0) {
      printf("0\n");
      return 0;
    }
    ans = ans * cnt % MOD;
    ++a[las];
  }
  printf("%lld\n", ans);
}