Problem p02845 (C++)

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

// int/long:		-2,147,483,648 - 2,147,483,648 (-2^31 <= int < 2^31)
// long/long long:	-9,223,372,036,854,775,808 - 9,223,372,036,854,775,807
// (-2^63 <= long < 2^63)

#define INF (1 << 29)
// 536,870,912

// lower_bound(A.begin(), A.end(), N)
// upper_bound(...

// A.erase(unique(A.begin(), A.end()), A.end())

// bit: &/and, |/or, ^/xor, ~/not

// getline(cin, String)

// Graph
// Warshall-Floyd: Distance between each node, N^3
// Dijkstra: Distance from the start node, N^2
// DFS(Depth-First Search)

#define MOD 1000000007

int main() {
  long N;
  cin >> N;
  /*
  vector<long> A(N, 0);
  for (long i=0; i<N; i++)
          cin >>A.at(i);
  */
  vector<vector<long>> C0;
  C0.push_back({0, 0, 0, 1});
  vector<vector<long>> C1;
  for (long i = 0; i < N; i++) {
    C1.erase(C1.begin(), C1.end());
    // long AA=A.at(i);
    long AA;
    cin >> AA;
    for (long j = 0; j < C0.size(); j++) {
      long c1 = C0.at(j).at(0);
      long c2 = C0.at(j).at(1);
      long c3 = C0.at(j).at(2);
      long cn = C0.at(j).at(3);
      if (AA == c1)
        C1.push_back({c1 + 1, c2, c3, cn});
      if (AA == c2)
        C1.push_back({c1, c2 + 1, c3, cn});
      if (AA == c3)
        C1.push_back({c1, c2, c3 + 1, cn});
    }
    sort(C1.begin(), C1.end());
    C0.erase(C0.begin(), C0.end());
    C0.push_back(C1.at(0));
    for (long k = 1; k < C1.size(); k++) {
      if ((C1.at(k).at(0) == C0.back().at(0)) &&
          (C1.at(k).at(1) == C0.back().at(1)) &&
          (C1.at(k).at(2) == C0.back().at(2))) {
        C0.back().at(3) += C1.at(k).at(3);
        C0.back().at(3) %= MOD;
      } else {
        C0.push_back(C1.at(k));
      }
    }
  }
  long S = 0;
  for (long i = 0; i < C0.size(); i++) {
    S += C0.at(i).at(3);
    S %= MOD;
  }
  cout << S << endl;
  return 0;
}


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

// int/long:		-2,147,483,648 - 2,147,483,648 (-2^31 <= int < 2^31)
// long/long long:	-9,223,372,036,854,775,808 - 9,223,372,036,854,775,807
// (-2^63 <= long < 2^63)

#define INF (1 << 29)
// 536,870,912

// lower_bound(A.begin(), A.end(), N)
// upper_bound(...

// A.erase(unique(A.begin(), A.end()), A.end())

// bit: &/and, |/or, ^/xor, ~/not

// getline(cin, String)

// Graph
// Warshall-Floyd: Distance between each node, N^3
// Dijkstra: Distance from the start node, N^2
// DFS(Depth-First Search)

#define MOD 1000000007

int main() {
  long N;
  cin >> N;
  /*
  vector<long> A(N, 0);
  for (long i=0; i<N; i++)
          cin >>A.at(i);
  */
  vector<vector<long>> C0;
  C0.push_back({0, 0, 0, 1});
  vector<vector<long>> C1;
  for (long i = 0; i < N; i++) {
    C1.erase(C1.begin(), C1.end());
    // long AA=A.at(i);
    long AA;
    cin >> AA;
    for (long j = 0; j < C0.size(); j++) {
      long c1 = C0.at(j).at(0);
      long c2 = C0.at(j).at(1);
      long c3 = C0.at(j).at(2);
      long cn = C0.at(j).at(3);
      if (AA == c1)
        C1.push_back({c1 + 1, c2, c3, cn});
      if (AA == c2)
        C1.push_back({c1, c2 + 1, c3, cn});
      if (AA == c3)
        C1.push_back({c1, c2, c3 + 1, cn});
    }
    if (0 == C1.size()) {
      cout << 0 << endl;
      return 0;
    }
    sort(C1.begin(), C1.end());
    C0.erase(C0.begin(), C0.end());
    C0.push_back(C1.at(0));
    for (long k = 1; k < C1.size(); k++) {
      if ((C1.at(k).at(0) == C0.back().at(0)) &&
          (C1.at(k).at(1) == C0.back().at(1)) &&
          (C1.at(k).at(2) == C0.back().at(2))) {
        C0.back().at(3) += C1.at(k).at(3);
        C0.back().at(3) %= MOD;
      } else {
        C0.push_back(C1.at(k));
      }
    }
  }
  long S = 0;
  for (long i = 0; i < C0.size(); i++) {
    S += C0.at(i).at(3);
    S %= MOD;
  }
  cout << S << endl;
  return 0;
}