Problem:
Three-way Branch

There is a grid that consists of W \times H cells. The upper-left-most cell is (1, 1).
You are standing on the cell of (1,1) and you are going to move to cell of (W, H).
You can only move to adjacent lower-left, lower or lower-right cells.

There are obstructions on several cells. You can not move to it. You cannot move out the grid, either.
Write a program that outputs the number of ways to reach (W,H) modulo 1,000,000,009.
You can assume that there is no obstruction at (1,1).

Input

The first line contains three integers, the width W, the height H, and the number of obstructions N.
(1 \leq W \leq 75, 2 \leq H \leq 10^{18}, 0 \leq N \leq 30)
Each of following N lines contains 2 integers, denoting the position of an obstruction (x_i, y_i).

The last test case is followed by a line containing three zeros.

Output

For each test case, print its case number and the number of ways to reach (W,H) modulo 1,000,000,009.

Sample Input

2 4 1
2 1
2 2 1
2 2
0 0 0

Output for the Sample Input

Case 1: 4
Case 2: 0

Solution in C++ (judge verdict: Wrong Answer)

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector< vector<ll> > Matrix;
typedef pair<ll, ll> P;

const ll mod = 1e9+7;

Matrix mul(Matrix A, Matrix B) {
	assert(A[0].size() == B.size());
	int l = A.size(), m = A[0].size(), n = B[0].size();
	Matrix res(l, vector<ll>(n, 0));
	for (int i = 0; i < l; ++i) {
		for (int j = 0; j < n; ++j) {
			for (int k = 0; k < m; ++k) {
				res[i][j] = (res[i][j] + A[i][k] * B[k][j]) % mod;
			}
		}
	}
	return res;
}
Matrix power(Matrix A, int n) {
	int m = A.size();
	Matrix res(m, vector<ll>(m, 0));
	for (int i = 0; i < m; ++i) res[i][i] = 1;
	for (ll i = 1; i <= n; i <<= 1) {
		if (n & i) {
			res = mul(res, A);
		}
		A = mul(A, A);
	}
	return res;
}

ll solve(int W, ll H, vector<P>& obs) {
	set<ll> nob;
	map<ll, set<ll> > m;
	for (int i = 0; i < obs.size(); ++i) {
		ll x = obs[i].first, y = obs[i].second;
		if (m.find(y) == m.end()) {
			set<ll> s; s.insert(x);
			m[y] = s;
		}
		else {
			m[y].insert(x);
		}
		nob.insert(y-1);
	}
	nob.insert(H-1);

	Matrix A(W, vector<ll>(W, 0));
	for (int i = 0; i < W; ++i) {
		for (int dx = -1; dx <= 1; ++dx) {
			int x = i+dx;
			if (0 <= x && x < W) {
				A[i][x] = 1;
			}
		}
	}

	Matrix d(W, vector<ll>(1, 0)); d[0][0] = 1;
	ll nowY = 0;
	while (nowY < H-1) {
		ll nextY = *nob.lower_bound(nowY);
		d = mul( power(A, nextY-nowY), d );
		nowY = nextY;
		if (nowY < H-1) {
			Matrix nd(W, vector<ll>(1, 0));
			for (int x = 0; x < W; ++x) {
				for (int dx = -1; dx <= 1; ++dx) {
					int nx = x+dx;
					ll ny = nowY+1;
					if (0 <= nx && nx < W && (m.find(ny) == m.end() || m[ny].count(nx) == 0)) {
						nd[nx][0] += d[x][0];
					}
				}
			}
			++nowY;
		}
	}
	return d[W-1][0];
}

int main() {
	int q = 1;
	ll W, H, N;
	while (cin >> W >> H >> N, W || H || N) {
		vector<P> obs;
		for (int i = 0; i < N; ++i) {
			ll x, y; cin >> x >> y; --x, --y;
			obs.push_back( P(x, y) );
		}
		cout << "Case " << q << ": " << solve(W, H, obs) << endl;
		++q;
	}
}