Problem p00491 (C++) — buggy submission, verdict: Runtime Error

#include <cstring>
#include <iostream>
using namespace std;

int main() {
  int n, m, d[1000], t[1000], type[4][100], c[4][100];
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> d[i] >> t[i];
  }
  for (int i = 0; i < m; i++) {
    if (d[i] == 1) {
      type[d[i]][1] = 1;
      goto Exit1;
    }
  }
  type[1][1] = 1;
  type[2][1] = 1;
  type[3][1] = 1;

Exit1:
  for (int i = 1; i <= 3; i++) {
    if (type[i][1] != 1) {
      type[i][1] = 0;
    }
  }
  type[1][2] = 0;
  type[2][2] = 0;
  type[3][2] = 0;
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < m; j++) {

      if (d[j] == i) {
        type[t[j]][2] = type[t[j]][1];
        type[t[j]][1] = 0;
        for (int k = 1; k <= 3; k++) {
          if (t[j] != k) {
            for (int l = 1; l <= 2; l++) {
              type[t[j]][1] += type[k][l];
              type[k][l] = 0;
            }
          }
        }
        goto Exit2;
      }
    }
    memset(c, 0, sizeof(c));
    for (int k = 1; k <= 3; k++) {
      for (int l = 1; l <= 3; l++) {
        if (k == l) {
          c[l][2] += type[k][1];
        } else {
          c[l][1] += type[k][1] + type[k][2];
        }
      }
    }
    for (int k = 1; k <= 3; k++) {
      type[k][1] = c[k][1];
      type[k][2] = c[k][2];
    }
  Exit2:
    for (int j = 1; j <= 3; j++) {
      for (int k = 1; k <= 2; k++) {
        type[j][k] = type[j][k] % 10000;
      }
    }
  }
  cout << (type[1][1] + type[1][2] + type[2][1] + type[2][2] + type[3][1] +
           type[3][2]) %
              10000
       << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <cstring>
#include <iostream>
using namespace std;

int main() {
  int n, m, d[1000], t[1000], type[4][100], c[4][100];
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> d[i] >> t[i];
  }
  for (int i = 0; i < m; i++) {
    if (d[i] == 1) {
      type[i][1] = 1;
      goto Exit1;
    }
  }
  type[1][1] = 1;
  type[2][1] = 1;
  type[3][1] = 1;

Exit1:
  for (int i = 1; i <= 3; i++) {
    if (type[i][1] != 1) {
      type[i][1] = 0;
    }
  }
  type[1][2] = 0;
  type[2][2] = 0;
  type[3][2] = 0;
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < m; j++) {

      if (d[j] == i) {
        type[t[j]][2] = type[t[j]][1];
        type[t[j]][1] = 0;
        for (int k = 1; k <= 3; k++) {
          if (t[j] != k) {
            for (int l = 1; l <= 2; l++) {
              type[t[j]][1] += type[k][l];
              type[k][l] = 0;
            }
          }
        }
        goto Exit2;
      }
    }
    memset(c, 0, sizeof(c));
    for (int k = 1; k <= 3; k++) {
      for (int l = 1; l <= 3; l++) {
        if (k == l) {
          c[l][2] += type[k][1];
        } else {
          c[l][1] += type[k][1] + type[k][2];
        }
      }
    }
    for (int k = 1; k <= 3; k++) {
      type[k][1] = c[k][1];
      type[k][2] = c[k][2];
    }
  Exit2:
    for (int j = 1; j <= 3; j++) {
      for (int k = 1; k <= 2; k++) {
        type[j][k] = type[j][k] % 100000000;
      }
    }
  }
  cout << (type[1][1] + type[1][2] + type[2][1] + type[2][2] + type[3][1] +
           type[3][2]) %
              10000
       << endl;
  return 0;
}