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 <string.h>
#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
#define ll long long
const int mod=1e9+7,N=80;
ll n,m,bug;
struct Matrix
{
	ll a[N][N];
	void org(int v)
	{
		memset(a,0,sizeof(a));
		for(int i=1;i<=n;i++)
		{
			a[i][i]=1;
			if(v) 
			{
				a[i][i-1]=a[i][i+1]=1;
			}
		}
	}
	Matrix operator *(Matrix t) const
	{
		Matrix ans;
		for(int i=1;i<=n;i++)
		{	
			for(int j=1;j<=n;j++)
			{
				ans.a[i][j]=0;
				for(int k=1;k<=n;k++) 
				ans.a[i][j]=(ans.a[i][j]+a[i][k]*t.a[k][j]%mod)%mod;
			}
		}
		return ans;
	}
	Matrix got(ll x)
	{
		Matrix ans;
		ans.org(0);
		// ans.out();
		Matrix mul;
		mul.org(1);
		// mul.out();
		while(x)
		{
			if(x&1) ans=ans*mul;
			x>>=1;
			mul=(mul*mul);
		}
		// ans.out();
		return ans;
	}
	void out()
	{
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=n;j++) printf("%lld ",a[i][j]);
			puts("");
		}
		puts("*******");
	}
};
set<ll> g;
vector<pair<ll,ll> > ar;
int main()
{
	 int ca=0;
	while(scanf("%lld%lld%lld",&n,&m,&bug),m+n+bug)
	{	
		g.clear();
		ar.clear();
		for(int i=0;i<bug;i++) 
		{
			ll x,y;scanf("%lld%lld",&x,&y);
			// cout<<x<<" "<<y<<endl;
			// printf("%I64d %I64d\n",x,y);
			ar.push_back(make_pair(x,y));
			g.insert(y);
		}
		g.erase(1);
		g.insert(m);
		ll a[N],b[N];
		memset(a,0,sizeof(a));
		a[1]=1;
		ll now=1;
		while(g.size())
		{
			ll to=*(g.begin());
			// cout<<to<<endl;
			g.erase(g.begin());
			Matrix t;
			t=t.got(to-now);
			//t.out();
			for(int i=1;i<=n;i++)
			{
				b[i]=0;
				for(int j=1;j<=n;j++)
				b[i]=(b[i]+t.a[i][j]*a[j])%mod;
			}
			// for(int i=1;i<=n;i++) printf("%d ",b[i]);
			// puts("");
			swap(a,b);
			// for(int i=1;i<=n;i++) printf("%lld ",a[i]);
			// puts("");
			// cout<<ar[0].second<<endl;
			for(int i=0;i<bug;i++) 
			if(ar[i].second==to) a[ar[i].first]=0;
			now=to;
			// for(int i=1;i<=n;i++) printf("%lld ",a[i]);
			// puts("");
		}
		printf("Case %d: %lld\n",++ca,a[n]);
	}
	return 0;
}