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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#define N 75
#define eps 1e-6
#define ll long long
#define db double
#define inf 0x3fffffff
#define A(a) printf("a==%d", a);
#define CC(x, y) memset(x, y, sizeof(x))
#define lson l, mid, rt << 1
#define rson mid + 1, r, (rt << 1) + 1
#define M 1000000009
using namespace std;
struct node {
  ll a[77][77];
} a, b, c, d, e;
int i, j, k, m, n, x, y, z;
ll h;
struct node2 {
  ll x, y;
} f[50];
node mul(node a, node b) {
  CC(d.a, 0);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      for (int k = 0; k < n; k++) {
        d.a[i][j] = (d.a[i][j] + a.a[i][k] * b.a[k][j]) % M;
      }
  return d;
}
node work(ll x) {
  c = b;
  CC(e.a, 0);
  for (i = 0; i < n; i++)
    e.a[i][i] = 1;
  while (x) {
    if (x & 1)
      e = mul(e, c);
    c = mul(c, c);
    x = x >> 1;
  }
  return e;
}
bool cmp(node2 a, node2 b) { return a.y < b.y; }
int main() {
  // freopen("in.txt","r",stdin);
  int cas = 1;
  while (scanf("%d%lld%d", &n, &h, &z), n) {
    CC(a.a, 0);
    CC(b.a, 0);
    a.a[0][0] = 1;
    for (i = 0; i < n; i++)
      for (j = 0; j < n; j++)
        if ((abs(j - i)) <= 1)
          b.a[i][j] = 1;
    c = b;
    CC(f, 0);
    f[0].y = 1;
    for (i = 1; i <= z; i++)
      scanf("%lld%lld", &f[i].x, &f[i].y);
    f[z + 1].y = h;
    f[z + 1].x = -1;
    sort(f, f + z + 2, cmp);
    for (int i = 1; i <= z + 1; i++) {
      ll x = f[i].y - f[i - 1].y;
      e = work(x);
      a = mul(a, e);
      if (f[i].x != -1)
        a.a[0][f[i].x - 1] = 0;
    }
    printf("Case %d: %lld\\n", cas++, a.a[0][n - 1]);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#define N 75
#define eps 1e-6
#define ll long long
#define db double
#define inf 0x3fffffff
#define A(a) printf("a==%d", a);
#define CC(x, y) memset(x, y, sizeof(x))
#define lson l, mid, rt << 1
#define rson mid + 1, r, (rt << 1) + 1
#define M 1000000009
using namespace std;
struct node {
  ll a[77][77];
} a, b, c, d, e;
int i, j, k, m, n, x, y, z;
ll h;
struct node2 {
  ll x, y;
} f[50];
node mul(node a, node b) {
  CC(d.a, 0);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      for (int k = 0; k < n; k++) {
        d.a[i][j] = (d.a[i][j] + a.a[i][k] * b.a[k][j]) % M;
      }
  return d;
}
node work(ll x) {
  c = b;
  CC(e.a, 0);
  for (i = 0; i < n; i++)
    e.a[i][i] = 1;
  while (x) {
    if (x & 1)
      e = mul(e, c);
    c = mul(c, c);
    x = x >> 1;
  }
  return e;
}
bool cmp(node2 a, node2 b) { return a.y < b.y; }
int main() {
  // freopen("in.txt","r",stdin);
  int cas = 1;
  while (scanf("%d%lld%d", &n, &h, &z), n) {
    a.a[0][0] = 1;
    for (i = 0; i < n; i++)
      for (j = 0; j < n; j++)
        if ((abs(j - i)) <= 1)
          b.a[i][j] = 1;
    c = b;
    f[0].y = 1;
    for (i = 1; i <= z; i++)
      scanf("%lld%lld", &f[i].x, &f[i].y);
    f[n + 1].y = h;
    f[n + 1].x = -1;
    sort(f, f + n + 2, cmp);
    for (int i = 1; i <= n + 1; i++) {
      ll x = f[i].y - f[i - 1].y;
      e = work(x);
      a = mul(a, e);
      if (f[i].x != -1)
        a.a[0][f[i].x - 1] = 0;
    }
    printf("Case %d: %lld\\n", cas++, a.a[0][n - 1]);
  }
  return 0;
}