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++14 (judge verdict: Accepted)

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
//#include<cctype>
#include<climits>
#include<iostream>
#include<string>
#include<vector>
#include<map>
//#include<list>
#include<queue>
#include<deque>
#include<algorithm>
//#include<numeric>
#include<utility>
//#include<memory>
#include<functional>
#include<cassert>
#include<set>
#include<stack>
#include<random>

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;

typedef long long number;
typedef vector<number> vec;
typedef vector<vec> matrix;

const int MOD = 1e9+9;

// O( n )
matrix Identity(int n) {
    matrix A(n, vec(n));
    for (int i = 0; i < n; ++i) A[i][i] = 1;
    return A;
}
// O( n^3 )
matrix mul(const matrix &A, const matrix &B) {
    matrix C(A.size(), vec(B[0].size()));
    for (int i = 0; i < (int)C.size(); ++i)
        for (int j = 0; j < (int)C[i].size(); ++j)
            for (int k = 0; k < (int)A[i].size(); ++k) {
                C[i][j] += A[i][k] * B[k][j];
                C[i][j] %= MOD;
            }
    return C;
}
// O( n^3 log e )
matrix pow(const matrix &A, ll e) {
    if (e == 0) return Identity(A.size());
    if (e == 1) return A;
    if (e % 2 == 0) {
        matrix tmp = pow(A, e/2);
        return mul(tmp, tmp);
    } else {
        matrix tmp = pow(A, e-1);
        return mul(A, tmp);
    }
}
// O(n)
number tr(const matrix &A) {
    number ans = 0;
    for (int i = 0; i < (int)A.size(); ++i)
        ans += A[i][i];
    return ans;
}
// O( n )
number inner_product(const vec &a, const vec &b) {
    number ans = 0;
    for (int i = 0; i < (int)a.size(); ++i)
        ans += a[i] * b[i];
    return ans;
}
// O( n^2 )
vec mul(const matrix &A, const vec &x) {
    vec y(A.size());
    for (int i = 0; i < (int)A.size(); ++i)
        for (int j = 0; j < (int)A[0].size(); ++j)
            (y[i] += (A[i][j] * x[j] % MOD)) %= MOD;
    return y;
}


const int MAXN = 33;
int W, N;
ll H;

ll solve() {
	vector<pair<ll, int> > P(N);
	for (int i = 0; i < N; i++) {
		cin >> P[i].second >> P[i].first;
		P[i].first--; P[i].second--;
	}
	sort(P.begin(), P.end());
	vec v(W);
	v[0] = 1;
	ll now = 0;
	for (int i = 0; i < N; ) {
		matrix mat(W, vec(W));
		for (int j = 0; j < W; j++) {
			if (j-1 >= 0) mat[j][j-1] = 1;
			mat[j][j] = 1;
			if (j+1 < W) mat[j][j+1] = 1;
		}
		v = mul(pow(mat, P[i].first-now), v);
		now = P[i].first;
		int ni = i;
		while (ni < N && P[i].first == P[ni].first)
			ni++;
		for (int j = i; j < ni; j++)
			v[P[j].second] = 0;
		i = ni;
	}
	matrix mat(W, vec(W));
	for (int i = 0; i < W; i++) {
		if (i-1 >= 0) mat[i][i-1] = 1;
		mat[i][i] = 1;
		if (i+1 < W) mat[i][i+1] = 1;
	}
	v = mul(pow(mat, H-1-now), v);
	return v[W-1];
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	for (int t = 1; ; t++) {
		cin >> W >> H >> N;
		if (W == 0) break;
		cout << "Case " << t << ": " << solve() << endl;
	}
	return 0;
}