Problem p02845 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, s, n) for (int i = (s); (n) > i; i++)
#define REP(i, n) rep(i, 0, n)
#define RANGE(x, a, b) (min(a, b) <= (x) && (x) <= max(a, b)) // hei
#define DUPLE(a, b, c, d)                                                      \
  (RANGE(a, c, d) || RANGE(b, c, d) || RANGE(c, a, b) || RANGE(d, a, b))
#define INCLU(a, b, c, d) (RANGE(a, c, d) && (b, c, d))
#define PW(x) ((x) * (x))
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MODU 1000000007LL
#define bitcheck(a, b) ((a >> b) & 1)
#define bitset(a, b) (a |= (1 << b))
#define bitunset(a, b) (a &= ~(1 << b))
#define MP(a, b) make_pair((a), (b))
#define Manh(a, b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int
#define PI 3.141592653589

#define izryt bool
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
pll Dir[4] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};

//[a, b)
#define Getsum(ar, a, b) (ar[b] - ar[a])
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)

struct Edge {
  int from, to;
  ll w;
  bool operator<(const Edge &rhs) const {
    return MP(w, MP(from, to)) < MP(rhs.w, MP(rhs.from, rhs.to));
  }
};
typedef vector<vector<Edge>> Graph;

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

ll factorial(ll n) { // 階乗
  ll ret = 1;
  rep(i, 1, n + 1) ret = (ret * i) % MODU;

  return ret;
}

ll mod_pow(ll x, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}
ll divmod(ll a, ll b, ll m) { // a/b(mod m)
                              // a/b (mod m)
                              // a*b^-1
                              // b^-1 * b = 1
                              // b^(m-1) = 1
                              // b^(m-2) * b = 1
                              // b^-1 = b^(m-2)

  // a/b = a * b^(m-2)

  return (a * 1LL * mod_pow(b, m - 2, m)) % m;
}
ll ncr(ll n, ll r) {
  return divmod(factorial(n), (factorial(r) * (factorial(n - r))) % MODU, MODU);
}

int n;
string base;
bool quest(int from) { // True:base
  printf("?");
  REP(i, n) { printf(" %d", (from + i) % (2 * n) + 1); }
  cout << endl;
  string str;
  cin >> str;
  return str == base;
}

bool quest(int from, int ad) { // True:base
  printf("?");
  REP(i, n - 1) { printf(" %d", (from + i) % (2 * n) + 1); }
  printf(" %d", ad + 1);

  cout << endl;
  string str;
  cin >> str;
  return str == base;
}

array<int, 3> c[100002] = {};
int cc[100002] = {};
signed main() {

#define INF 10000000
  int n;
  cin >> n;
  vector<int> a(n);
  Fill(c, INF);
  REP(i, n) {
    cin >> a[i];
    c[a[i]][cc[a[i]]] = i;
    cc[a[i]]++;
  }
  rep(i, 1, 100002) {
    if (cc[i] > cc[i - 1] || cc[i] > 3) {
      cout << 0 << endl;
      return 0;
    }
  }

  map<array<int, 3>, ll> dp, dp2;
  dp[{-1, -1, -1}] = 1LL;
  REP(i, n + 1) {
    for (auto itr : dp) {
      sort(ALL(c[i]));
      do {
        bool ok = true;
        REP(j, 3) {
          if (itr.first[j] == INF && c[i][j] == INF)
            continue;
          if (itr.first[j] >= c[i][j]) {
            ok = false;
          }
        }
        if (ok) {
          dp2[c[i]] += itr.second;
          dp2[c[i]] %= MODU;
        }
      } while (next_permutation(ALL(c[i])));
    }
    swap(dp, dp2);
    dp2.clear();
  }

  ll ans = 0;
  for (auto itr : dp) {
    ans += itr.second;
    ans %= MODU;
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, s, n) for (int i = (s); (n) > i; i++)
#define REP(i, n) rep(i, 0, n)
#define RANGE(x, a, b) (min(a, b) <= (x) && (x) <= max(a, b)) // hei
#define DUPLE(a, b, c, d)                                                      \
  (RANGE(a, c, d) || RANGE(b, c, d) || RANGE(c, a, b) || RANGE(d, a, b))
#define INCLU(a, b, c, d) (RANGE(a, c, d) && (b, c, d))
#define PW(x) ((x) * (x))
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MODU 1000000007LL
#define bitcheck(a, b) ((a >> b) & 1)
#define bitset(a, b) (a |= (1 << b))
#define bitunset(a, b) (a &= ~(1 << b))
#define MP(a, b) make_pair((a), (b))
#define Manh(a, b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int
#define PI 3.141592653589

#define izryt bool
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
pll Dir[4] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};

//[a, b)
#define Getsum(ar, a, b) (ar[b] - ar[a])
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)

struct Edge {
  int from, to;
  ll w;
  bool operator<(const Edge &rhs) const {
    return MP(w, MP(from, to)) < MP(rhs.w, MP(rhs.from, rhs.to));
  }
};
typedef vector<vector<Edge>> Graph;

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

ll factorial(ll n) { // 階乗
  ll ret = 1;
  rep(i, 1, n + 1) ret = (ret * i) % MODU;

  return ret;
}

ll mod_pow(ll x, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}
ll divmod(ll a, ll b, ll m) { // a/b(mod m)
                              // a/b (mod m)
                              // a*b^-1
                              // b^-1 * b = 1
                              // b^(m-1) = 1
                              // b^(m-2) * b = 1
                              // b^-1 = b^(m-2)

  // a/b = a * b^(m-2)

  return (a * 1LL * mod_pow(b, m - 2, m)) % m;
}
ll ncr(ll n, ll r) {
  return divmod(factorial(n), (factorial(r) * (factorial(n - r))) % MODU, MODU);
}

int n;
string base;
bool quest(int from) { // True:base
  printf("?");
  REP(i, n) { printf(" %d", (from + i) % (2 * n) + 1); }
  cout << endl;
  string str;
  cin >> str;
  return str == base;
}

bool quest(int from, int ad) { // True:base
  printf("?");
  REP(i, n - 1) { printf(" %d", (from + i) % (2 * n) + 1); }
  printf(" %d", ad + 1);

  cout << endl;
  string str;
  cin >> str;
  return str == base;
}

array<int, 3> c[100002] = {};
int cc[100002] = {};
signed main() {

#define INF 10000000
  int n;
  cin >> n;
  vector<int> a(n);
  Fill(c, INF);
  REP(i, n) {
    cin >> a[i];
    c[a[i]][cc[a[i]]] = i;
    if (cc[a[i]] == 3) {
      cout << 0 << endl;
      return 0;
    }
    cc[a[i]]++;
  }
  rep(i, 1, 100002) {
    if (cc[i] > cc[i - 1] || cc[i] > 3) {
      cout << 0 << endl;
      return 0;
    }
  }

  map<array<int, 3>, ll> dp, dp2;
  dp[{-1, -1, -1}] = 1LL;
  REP(i, n + 1) {
    for (auto itr : dp) {
      sort(ALL(c[i]));
      do {
        bool ok = true;
        REP(j, 3) {
          if (itr.first[j] == INF && c[i][j] == INF)
            continue;
          if (itr.first[j] >= c[i][j]) {
            ok = false;
          }
        }
        if (ok) {
          dp2[c[i]] += itr.second;
          dp2[c[i]] %= MODU;
        }
      } while (next_permutation(ALL(c[i])));
    }
    swap(dp, dp2);
    dp2.clear();
  }

  ll ans = 0;
  for (auto itr : dp) {
    ans += itr.second;
    ans %= MODU;
  }
  cout << ans << endl;

  return 0;
}