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

Submitted solutions:

code_snippet: #include <cstdio>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <map>
#define llint long long
#define MOD 1000000009

using namespace std;

const int maxn = 75; //必须加 const，否则编译错误

int n, N;
llint m;
map<llint, vector<llint> > s;

class Matrix {
public:
	llint val[maxn][maxn];
	Matrix() {
		memset(val, 0, sizeof(val));
	}

	Matrix operator*(const Matrix& c) const {
		Matrix res;
		for (int i = 0; i < n; ++i)
			for (int j = 0; j < n; ++j)
				for (int k = 0; k < n; ++k) {
					res.val[i][j] += val[i][k] * c.val[k][j];
					res.val[i][j] = (res.val[i][j] + MOD) % MOD; //防止矩阵元素变为负数，若不需要，去掉"+MOD"
				}
		return res;
	}

	Matrix& operator*=(const Matrix& c) {
		*this = *this * c;
		return *this;
	}

	Matrix Pow(llint k) { //返回one^k
		Matrix res = Zero();
		Matrix step = One();
		while (k) {
			if (k & 1)
				res *= step;
			k >>= 1;
			step *= step;
		}
		return res;
	}

	Matrix Zero() const {
		Matrix res;
		for (int i = 0; i < maxn; ++i)
			res.val[i][i] = 1;
		return res;
	}

	Matrix One() const {
		Matrix res;
		res.val[0][0] = res.val[0][1] = 1;
		res.val[n-1][n-1] = res.val[n-1][n-2] = 1;
		for (int i = 1; i < n - 1; ++i) {
			res.val[i][i-1] = res.val[i][i] = res.val[i][i + 1] = 1;
		}
		return res;
	}
};

int main() {
	int kcase = 0;
	while (scanf("%d%lld%d", &n, &m, &N) && n) {
		s.clear();
		for (int i = 0; i < N; ++i) {
			llint x, y;
			scanf("%lld%lld", &x, &y);
			s[--y].push_back(--x);
		}
		if (n == 1) {
			if (N) {
				printf("Case %d: 0\n", ++kcase);
			} else {
				printf("Case %d: 1\n", ++kcase);
			}
			continue;
		}
		Matrix res;
		res.val[0][0] = 1;
		llint now = 0;
		for (map<llint, vector<llint> >::iterator it = s.begin(); it != s.end(); ++it) {
            //printf("%I64d\n", it -> first);
			if (it -> first == 0) continue;
			res *= res.Pow((it -> first) - now);
			now = it -> first;
			for (int i = 0; i < (int)(it -> second).size(); ++i) {
				res.val[0][(it -> second)[i]] = 0;
			}
		}
		res *= res.Pow(m - 1 - now);
		printf("Case %d: %lld\n", ++kcase, res.val[0][n - 1]);
	}
    return 0;
}
accuracy: 1
time_ms: 3290
memory_kb: 3136
final_rank: 8

code_snippet: #include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

const long long MOD = 1000000009;

struct Step
{
    long long Marx[75][75];
    long long H;
    long long W;
};
struct Obstruction
{
    long long x;
    long long y;
};

Step StepByStep(Step a,Step b)
{
    Step ret;
    ret.H = a.H + b.H - 1;
    ret.W = a.W;

    for(int i=0;i<ret.W;++i)
    {
        for(int j=0;j<ret.W;++j)
        {
            ret.Marx[i][j] = 0;
            for(int k=0;k<ret.W;++k)
            {
                ret.Marx[i][j] += a.Marx[i][k]*b.Marx[k][j];
                ret.Marx[i][j] = ret.Marx[i][j]%MOD;
            }
        }
    }

    return ret;
}

long long InitStep(Step step[],long long W,long long H)
{
    long long h = 3;
    long long Tot = 1;
    step[0].W = W;
    step[0].H = 2;
    for(int i=0;i<W;++i)
    {
        for(int j=0;j<W;++j)
        {
            if(i==j||i==j-1||i==j+1)
            step[0].Marx[i][j] = 1;
            else
            step[0].Marx[i][j] = 0;
        }
    }

    while(h<=H)
    {
        step[Tot] = StepByStep(step[Tot-1],step[Tot-1]);
        ++Tot;
        h = 2*h-1;
    }

    return Tot;
}

Step CombineStep(long long H,long long W,Step step[],long long N) //H 为高度
{
    Step way;
    way.H = 1;
    way.W = W;
    for(int i=0;i<W;++i)
    for(int j=0;j<W;++j)
    {
        if(i==j)
        way.Marx[i][j] = 1;
        else way.Marx[i][j] = 0;
    }

    long long h = 1;
    for(int i=N-1;i>=0;--i)
    {
        if(h+step[i].H-1<=H)
        {
            way = StepByStep(way,step[i]);
            h += step[i].H-1;
        }
    }
    return way;
}

int cmp(Obstruction obs1,Obstruction obs2)
{
    return obs1.y<obs2.y;
}

long long W,H,N;
Step step[100],way;
long long Tot;
Obstruction obs[30];

int main()
{
    //freopen("in.txt","r",stdin);
    int iCase = 1;
    while(scanf("%lld %lld %lld",&W,&H,&N)!=EOF)
    {
        if(W==0&&H==0&&N==0)
        break;

        Tot = InitStep(step,W,H);

        for(int i=0;i<N;++i)
        {
            scanf("%lld %lld",&obs[i].x,&obs[i].y);
        }
        sort(obs,obs+N,cmp);
        if(N==0)
        {
            way = CombineStep(H,W,step,Tot);
        }
        else
        {
            way.H = 1;
            way.W = W;
            for(int i=0;i<W;++i)
            for(int j=0;j<W;++j)
            {
                if(i==j)
                way.Marx[i][j] = 1;
                else way.Marx[i][j] = 0;
            }

            for(int i=0;i<=N;++i)
            {
                if(i==0)
                {
                    way = StepByStep(way,CombineStep(obs[i].y,W,step,Tot));
                    for(int j=0;j<W;++j)
                    {
                        way.Marx[j][obs[i].x-1] = 0;
                    }
                }
                else if(i==N)
                {
                    way = StepByStep(way,CombineStep(H - obs[N-1].y+1,W,step,Tot));
                }
                else
                {
                    way = StepByStep(way,CombineStep(obs[i].y-obs[i-1].y+1,W,step,Tot));
                    for(int j=0;j<W;++j)
                    {
                        way.Marx[j][obs[i].x-1] = 0;
                    }
                }
            }

        }

        printf("Case %d: %lld\n",iCase,way.Marx[0][W-1]%MOD);
        ++iCase;
    }
    return 0;
}
accuracy: 1
time_ms: 1230
memory_kb: 7944
final_rank: 8

code_snippet: #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <cmath>
using namespace std;
#define MOD 1000000009
long long w,h,n;
struct Matrix{
    long long n,m[80][80];
    Matrix  operator * (const Matrix &b)const{
        Matrix c;
        memset(c.m,0,sizeof c.m);
        c.n=n;
        for (int i=0;i<n;i++)
        for (int j=0;j<n;j++)
        for (int k=0;k<n;k++){
            c.m[i][j]=(c.m[i][j]+(m[i][k]*b.m[k][j])%MOD)%MOD;
        }
        return c;
    }
};
Matrix ones;
Matrix Pow(const Matrix &a,unsigned long long n){
    Matrix t=ones,y=a;
    while (n){
        if (n & 1) t = (t * y) ;
        y = (y * y) ;
        n /= 2;
    }
    return t;
}
Matrix A;
long long x[80];
long long k,p,q;
struct node{
    long long a,b;
};
node Pos[80];
int cmp(node a,node b){
    return a.b<b.b;
}
void get_x(Matrix a){
    long long y[80];
    memset(y,0,sizeof y);
    for (int i=0;i<a.n;i++){
        for (int j=0;j<a.n;j++){
            y[i]=(y[i]+(x[j]*a.m[i][j])%MOD)%MOD;
        }
    }
    for (int i=0;i<a.n;i++)
        x[i]=y[i];
}
int main(){
    int i,j;
    int d=0;
    while (scanf("%lld%lld%lld",&w,&h,&n),w||h||n){
        memset(ones.m,0,sizeof ones.m);
        for (i=0;i<w;i++)
            ones.m[i][i]=1;
        ones.n=w;
        for (i=0;i<n;i++)
            scanf("%lld%lld",&Pos[i].a,&Pos[i].b); //列向量
        sort(Pos,Pos+n,cmp);
        //if ((w==0)&&(h==0)&&(n==0)) return 0;
        memset(x,0,sizeof x);
        x[0]=1;
        memset(A.m,0,sizeof A.m);
        A.n=w;
        A.m[0][0]=1;
        A.m[0][1]=1;
        for (i=1;i<w;i++){
            A.m[i][i]=1;
            A.m[i][i-1]=1;
            A.m[i][i+1]=1;
        }
        k=0;
        j=0;
        while (j<n){
            p=Pos[j].b-1;
            if (p==k){
                x[Pos[j].a-1]=0;
            }
            else{
                get_x(Pow(A,p-k));
                k=p;
                x[Pos[j].a-1]=0;
            }
            j++;
        }
        if (k<h-1){
            get_x(Pow(A,h-1-k));
        }
        printf("Case %d: %lld\n",++d,x[w-1]);
    }
}
accuracy: 1
time_ms: 2980
memory_kb: 3912
final_rank: 15

code_snippet: #include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <cstring>
using namespace std;
#define MOD 1000000009
struct Matrix{
    long long n;

    Matrix(){
        memset(m,0,sizeof m);
    };
    long long m[80][80];
    Matrix operator * (const Matrix &b) const{
        Matrix c;
        for (long long i=0;i<n;i++)
        for (long long j=0;j<n;j++){
            c.m[i][j]=0;
            for (long long k=0;k<n;k++)
                c.m[i][j]=(c.m[i][j]+(m[i][k]*b.m[k][j])%MOD)%MOD;
        }
        c.n=n;
        return c;
    }
};
Matrix ones;
Matrix Pow(const Matrix &a,long long n){
    Matrix t=ones,y=a;
    while (n){
        if (n & 1) t = (t * y) ;
        y = (y * y) ;
        n /= 2;
    }
    return t;
}
struct node{
    long long a,b;
}Pos[80];
bool cmp(node a,node b){
    return a.b<b.b;
}
Matrix A,B;
long long x[80];
void get_x(Matrix A){
    long long y[80];
    //printf("!!!");
    memset(y,0,sizeof y);
    for (long long i=0;i<A.n;i++){
        for (long long j=0;j<A.n;j++){
            y[i]=(y[i]+(x[j]*A.m[i][j])%MOD)%MOD;
        }
    }
    for (long long i=0;i<A.n;i++)
        x[i]=y[i];
}
long long sum;
long long w,n;
long long h;
long long k,p,q;
int main(){
    int i,j;
    int d=0;
    while (scanf("%lld%lld%lld",&w,&h,&n),w||h||n){
        memset(ones.m,0,sizeof ones.m);
        ones.n=w;
        for (i=0;i<w;i++)
            ones.m[i][i]=1;
        for (i=0;i<n;i++){
            scanf("%lld%lld",&Pos[i].a,&Pos[i].b);
            //Pos[i].a--;Pos[i].b--;
        }
        sort(Pos,Pos+n,cmp);

        memset(x,0,sizeof x);
        x[0]=1;
        memset(A.m,0,sizeof A.m);

        A.n=w;
        A.m[0][0]=1; A.m[0][1]=1;
        for (i=1;i<w;i++){
            A.m[i][i-1]=1;
            A.m[i][i]=1;
            A.m[i][i+1]=1;
        }
        //for (i=0;i<w;i++)
        //    x[i]=0;//行向量
        k=0;
        j=0;
        //k=0;
        //j=0;
        /*
        sort(Pos,Pos+n,cmp);
        //if ((w==0)&&(h==0)&&(n==0)) return 0;
        memset(x,0,sizeof x);
        x[0]=1;
        memset(A.m,0,sizeof A.m);

        A.n=w;
        A.m[0][0]=1;
        A.m[0][1]=1;
        for (i=1;i<w;i++){
            A.m[i][i]=1;
            A.m[i][i-1]=1;
            A.m[i][i+1]=1;
        }
        k=0;
        j=0;
        */

        // ****************************************
        /*
        int i,j;
    int d=0;
    while (scanf("%lld%lld%lld",&w,&h,&n),w||h||n){
        memset(ones.m,0,sizeof ones.m);
        for (i=0;i<w;i++)
            ones.m[i][i]=1;
        ones.n=w;
        for (i=0;i<n;i++)
            scanf("%lld%lld",&Pos[i].a,&Pos[i].b);
        sort(Pos,Pos+n,cmp);
        //if ((w==0)&&(h==0)&&(n==0)) return 0;
        memset(x,0,sizeof x);
        x[0]=1;
        memset(A.m,0,sizeof A.m);
        A.n=w;
        A.m[0][0]=1;
        A.m[0][1]=1;
        for (i=1;i<w;i++){
            A.m[i][i]=1;
            A.m[i][i-1]=1;
            A.m[i][i+1]=1;
        }
        k=0;
        j=0;
        */
        //****************************************
        //printf("!!!");
        while (j<n){
            p=Pos[j].b-1;
            if (k==p){
                x[Pos[j].a-1]=0;
            }
            else{
                get_x(Pow(A,p-k));
                k=p;
                x[Pos[j].a-1]=0;
            }
            //printf("%d",k);
            //for (int i=0;i<w;i++)
            //    printf("%5d",x[i]);
            //printf("\n");
            j++;

        }
        //printf("!!!");
        if (k<h-1){

//            B=Pow(A,h-1-k);printf("#!!!");
            get_x(Pow(A,h-1-k));
        }
        printf("Case %lld: %lld\n",++d,(x[w-1]%MOD+MOD)%MOD);
    }
}
accuracy: 1
time_ms: 3000
memory_kb: 3916
final_rank: 20

code_snippet: #include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <cstring>
using namespace std;
#define MOD 1000000009
struct Matrix{
    long long n;

    Matrix(){
        memset(m,0,sizeof m);
    };
    long long m[80][80];
    Matrix operator * (const Matrix &b) const{
        Matrix c;
        for (long long i=0;i<n;i++)
        for (long long j=0;j<n;j++){
            c.m[i][j]=0;
            for (long long k=0;k<n;k++)
                c.m[i][j]=(c.m[i][j]+(m[i][k]*b.m[k][j])%MOD)%MOD;
        }
        c.n=n;
        return c;
    }
};
Matrix ones;
Matrix Pow(const Matrix &a,long long n){
    Matrix t=ones,y=a;
    while (n){
        if (n & 1) t = (t * y) ;
        y = (y * y) ;
        n /= 2;
    }
    return t;
}
struct node{
    long long a,b;
}Pos[80];
bool cmp(node a,node b){
    return a.b<b.b;
}
Matrix A,B;
long long x[80];
void get_x(Matrix A){
    long long y[80];
    //printf("!!!");
    memset(y,0,sizeof y);
    for (long long i=0;i<A.n;i++){
        for (long long j=0;j<A.n;j++){
            y[i]=(y[i]+(x[j]*A.m[i][j])%MOD)%MOD;
        }
    }
    for (long long i=0;i<A.n;i++)
        x[i]=y[i];
}
long long sum;
long long w,n;
long long h;
long long k,p,q;
int main(){
    int i,j;
    int d=0;
    while (scanf("%lld%lld%lld",&w,&h,&n),w||h||n){
        memset(ones.m,0,sizeof ones.m);
        ones.n=w;
        for (i=0;i<w;i++)
            ones.m[i][i]=1;
        for (i=0;i<n;i++){
            scanf("%lld%lld",&Pos[i].a,&Pos[i].b);
            //Pos[i].a--;Pos[i].b--;
        }
        sort(Pos,Pos+n,cmp);

        memset(x,0,sizeof x);
        x[0]=1;
        memset(A.m,0,sizeof A.m);

        A.n=w;
        A.m[0][0]=1; A.m[0][1]=1;
        for (i=1;i<w;i++){
            A.m[i][i-1]=1;
            A.m[i][i]=1;
            A.m[i][i+1]=1;
        }
        //for (i=0;i<w;i++)
        //    x[i]=0;//列向量
        k=0;
        j=0;
        //k=0;
        //j=0;
        /*
        sort(Pos,Pos+n,cmp);
        //if ((w==0)&&(h==0)&&(n==0)) return 0;
        memset(x,0,sizeof x);
        x[0]=1;
        memset(A.m,0,sizeof A.m);

        A.n=w;
        A.m[0][0]=1;
        A.m[0][1]=1;
        for (i=1;i<w;i++){
            A.m[i][i]=1;
            A.m[i][i-1]=1;
            A.m[i][i+1]=1;
        }
        k=0;
        j=0;
        */

        // ****************************************
        /*
        int i,j;
    int d=0;
    while (scanf("%lld%lld%lld",&w,&h,&n),w||h||n){
        memset(ones.m,0,sizeof ones.m);
        for (i=0;i<w;i++)
            ones.m[i][i]=1;
        ones.n=w;
        for (i=0;i<n;i++)
            scanf("%lld%lld",&Pos[i].a,&Pos[i].b);
        sort(Pos,Pos+n,cmp);
        //if ((w==0)&&(h==0)&&(n==0)) return 0;
        memset(x,0,sizeof x);
        x[0]=1;
        memset(A.m,0,sizeof A.m);
        A.n=w;
        A.m[0][0]=1;
        A.m[0][1]=1;
        for (i=1;i<w;i++){
            A.m[i][i]=1;
            A.m[i][i-1]=1;
            A.m[i][i+1]=1;
        }
        k=0;
        j=0;
        */
        //****************************************
        //printf("!!!");
        while (j<n){
            p=Pos[j].b-1;
            if (k==p){
                x[Pos[j].a-1]=0;
            }
            else{
                get_x(Pow(A,p-k));
                k=p;
                x[Pos[j].a-1]=0;
            }
            //printf("%d",k);
            //for (int i=0;i<w;i++)
            //    printf("%5d",x[i]);
            //printf("\n");
            j++;

        }
        //printf("!!!");
        if (k<h-1){

//            B=Pow(A,h-1-k);printf("#!!!");
            get_x(Pow(A,h-1-k));
        }
        printf("Case %lld: %lld\n",++d,(x[w-1]%MOD+MOD)%MOD);
    }
}
accuracy: 1
time_ms: 2980
memory_kb: 3916
final_rank: 16

code_snippet: #include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <cstring>
using namespace std;
#define MOD 1000000009
struct Matrix{
    long long n;

    Matrix(){
        memset(m,0,sizeof m);
    };
    long long m[80][80];
    Matrix operator * (const Matrix &b) const{
        Matrix c;
        for (long long i=0;i<n;i++)
        for (long long j=0;j<n;j++){
            c.m[i][j]=0;
            for (long long k=0;k<n;k++)
                c.m[i][j]=(c.m[i][j]+(m[i][k]*b.m[k][j])%MOD)%MOD;
        }
        c.n=n;
        return c;
    }
};
Matrix ones;
Matrix Pow(const Matrix &a,long long n){
    Matrix t=ones,y=a;
    while (n){
        if (n & 1) t = (t * y) ;
        y = (y * y) ;
        n /= 2;
    }
    return t;
}
struct node{
    long long a,b;
}Pos[80];
bool cmp(node a,node b){
    return a.b<b.b;
}
Matrix A,B;
long long x[80];
void get_x(Matrix A){
    long long y[80];
    //printf("!!!");
    memset(y,0,sizeof y);
    for (long long i=0;i<A.n;i++){
        for (long long j=0;j<A.n;j++){
            y[i]=(y[i]+(x[j]*A.m[i][j])%MOD)%MOD;
        }
    }
    for (long long i=0;i<A.n;i++)
        x[i]=y[i];
}
long long sum;
long long w,n;
long long h;
long long k,p,q;
int main(){
    int i,j;
    int d=0;
    while (scanf("%lld%lld%lld",&w,&h,&n),w||h||n){
        memset(ones.m,0,sizeof ones.m);
        ones.n=w;
        for (i=0;i<w;i++)
            ones.m[i][i]=1;
        for (i=0;i<n;i++){
            scanf("%lld%lld",&Pos[i].a,&Pos[i].b);
            //Pos[i].a--;Pos[i].b--;
        }
        sort(Pos,Pos+n,cmp);

        memset(x,0,sizeof x);
        x[0]=1;
        memset(A.m,0,sizeof A.m);

        A.n=w;
        A.m[0][0]=1; A.m[0][1]=1;
        for (i=1;i<w;i++){
            A.m[i][i-1]=1;
            A.m[i][i]=1;
            A.m[i][i+1]=1;
        }
        //for (i=0;i<w;i++)
        //    x[i]=0;//列向量
        k=0;
        //j=0;
        //k=0;
        j=0;
        /*
        sort(Pos,Pos+n,cmp);
        //if ((w==0)&&(h==0)&&(n==0)) return 0;
        memset(x,0,sizeof x);
        x[0]=1;
        memset(A.m,0,sizeof A.m);

        A.n=w;
        A.m[0][0]=1;
        A.m[0][1]=1;
        for (i=1;i<w;i++){
            A.m[i][i]=1;
            A.m[i][i-1]=1;
            A.m[i][i+1]=1;
        }
        k=0;
        j=0;
        */

        // ****************************************
        /*
        int i,j;
    int d=0;
    while (scanf("%lld%lld%lld",&w,&h,&n),w||h||n){
        memset(ones.m,0,sizeof ones.m);
        for (i=0;i<w;i++)
            ones.m[i][i]=1;
        ones.n=w;
        for (i=0;i<n;i++)
            scanf("%lld%lld",&Pos[i].a,&Pos[i].b);
        sort(Pos,Pos+n,cmp);
        //if ((w==0)&&(h==0)&&(n==0)) return 0;
        memset(x,0,sizeof x);
        x[0]=1;
        memset(A.m,0,sizeof A.m);
        A.n=w;
        A.m[0][0]=1;
        A.m[0][1]=1;
        for (i=1;i<w;i++){
            A.m[i][i]=1;
            A.m[i][i-1]=1;
            A.m[i][i+1]=1;
        }
        k=0;
        j=0;
        */
        //****************************************
        //printf("!!!");
        while (j<n){
            p=Pos[j].b-1;
            if (k==p){
                x[Pos[j].a-1]=0;
            }
            else{
                get_x(Pow(A,p-k));
                k=p;
                x[Pos[j].a-1]=0;
            }
            //printf("%d",k);
            //for (int i=0;i<w;i++)
            //    printf("%5d",x[i]);
            //printf("\n");
            j++;

        }
        //printf("!!!");
        if (k<h-1){

//            B=Pow(A,h-1-k);printf("#!!!");
            get_x(Pow(A,h-1-k));
        }
        printf("Case %lld: %lld\n",++d,(x[w-1]%MOD+MOD)%MOD);
    }
}
accuracy: 1
time_ms: 2980
memory_kb: 3920
final_rank: 18

code_snippet: #include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <cstring>
using namespace std;
#define MOD 1000000009
struct Matrix{
    long long n;

    Matrix(){
        memset(m,0,sizeof m);
    };
    long long m[80][80];
    Matrix operator * (const Matrix &b) const{
        Matrix c;
        for (long long i=0;i<n;i++)
        for (long long j=0;j<n;j++){
            c.m[i][j]=0;
            for (long long k=0;k<n;k++)
                c.m[i][j]=(c.m[i][j]+(m[i][k]*b.m[k][j])%MOD)%MOD;
        }
        c.n=n;
        return c;
    }
};
Matrix ones;
Matrix Pow(const Matrix &a,long long n){
    Matrix t=ones,y=a;
    while (n){
        if (n & 1) t = (t * y) ;
        y = (y * y) ;
        n /= 2;
    }
    return t;
}
struct node{
    long long a,b;
}Pos[80];
bool cmp(node a,node b){
    return a.b<b.b;
}
Matrix A,B;
long long x[80];
void get_x(Matrix A){
    long long y[80];
    //printf("!!!");
    memset(y,0,sizeof y);
    for (long long i=0;i<A.n;i++){
        for (long long j=0;j<A.n;j++){
            y[i]=(y[i]+(x[j]*A.m[i][j])%MOD)%MOD;
        }
    }
    for (long long i=0;i<A.n;i++)
        x[i]=y[i];
}
long long sum;
long long w,n;
long long h;
long long k,p,q;
int main(){
    int i,j;
    int d=0;
    while (scanf("%lld%lld%lld",&w,&h,&n),w||h||n){
        memset(ones.m,0,sizeof ones.m);
        ones.n=w;
        for (i=0;i<w;i++)
            ones.m[i][i]=1;
        for (i=0;i<n;i++){
            scanf("%lld%lld",&Pos[i].a,&Pos[i].b);
            //Pos[i].a--;Pos[i].b--;
        }
        sort(Pos,Pos+n,cmp);

        memset(x,0,sizeof x);
        x[0]=1;
        memset(A.m,0,sizeof A.m);

        A.n=w;
        A.m[0][0]=1; A.m[0][1]=1;
        for (i=1;i<w;i++){
            A.m[i][i-1]=1;
            A.m[i][i]=1;
            A.m[i][i+1]=1;
        }
        //for (i=0;i<w;i++)
        //    x[i]=0;//列向量
        k=0;
        j=0;
        /*
        sort(Pos,Pos+n,cmp);
        //if ((w==0)&&(h==0)&&(n==0)) return 0;
        memset(x,0,sizeof x);
        x[0]=1;
        memset(A.m,0,sizeof A.m);

        A.n=w;
        A.m[0][0]=1;
        A.m[0][1]=1;
        for (i=1;i<w;i++){
            A.m[i][i]=1;
            A.m[i][i-1]=1;
            A.m[i][i+1]=1;
        }
        k=0;
        j=0;
        */

        // ****************************************
        /*
        int i,j;
    int d=0;
    while (scanf("%lld%lld%lld",&w,&h,&n),w||h||n){
        memset(ones.m,0,sizeof ones.m);
        for (i=0;i<w;i++)
            ones.m[i][i]=1;
        ones.n=w;
        for (i=0;i<n;i++)
            scanf("%lld%lld",&Pos[i].a,&Pos[i].b);
        sort(Pos,Pos+n,cmp);
        //if ((w==0)&&(h==0)&&(n==0)) return 0;
        memset(x,0,sizeof x);
        x[0]=1;
        memset(A.m,0,sizeof A.m);
        A.n=w;
        A.m[0][0]=1;
        A.m[0][1]=1;
        for (i=1;i<w;i++){
            A.m[i][i]=1;
            A.m[i][i-1]=1;
            A.m[i][i+1]=1;
        }
        k=0;
        j=0;
        */
        //****************************************
        //printf("!!!");
        while (j<n){
            p=Pos[j].b-1;
            if (k==p){
                x[Pos[j].a-1]=0;
            }
            else{
                get_x(Pow(A,p-k));
                k=p;
                x[Pos[j].a-1]=0;
            }
            //printf("%d",k);
            //for (int i=0;i<w;i++)
            //    printf("%5d",x[i]);
            //printf("\n");
            j++;

        }
        //printf("!!!");
        if (k<h-1){

//            B=Pow(A,h-1-k);printf("#!!!");
            get_x(Pow(A,h-1-k));
        }
        printf("Case %lld: %lld\n",++d,(x[w-1]%MOD+MOD)%MOD);
    }
}
accuracy: 1
time_ms: 2970
memory_kb: 3608
final_rank: 3

code_snippet: #include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <cstring>
using namespace std;
#define MOD 1000000009
struct Matrix{
    long long n;

    Matrix(){
        memset(m,0,sizeof m);
    };
    long long m[80][80];
    Matrix operator * (const Matrix &b) const{
        Matrix c;
        for (long long i=0;i<n;i++)
        for (long long j=0;j<n;j++){
            c.m[i][j]=0;
            for (long long k=0;k<n;k++)
                c.m[i][j]=(c.m[i][j]+(m[i][k]*b.m[k][j])%MOD)%MOD;
        }
        c.n=n;
        return c;
    }
};
Matrix ones;
Matrix Pow(const Matrix &a,long long n){
    Matrix t=ones,y=a;
    while (n){
        if (n & 1) t = (t * y) ;
        y = (y * y) ;
        n /= 2;
    }
    return t;
}
struct node{
    long long a,b;
}Pos[80];
bool cmp(node a,node b){
    return a.b<b.b;
}
Matrix A,B;
long long x[80];
void get_x(Matrix A){
    long long y[80];
    //printf("!!!");
    memset(y,0,sizeof y);
    for (long long i=0;i<A.n;i++){
        for (long long j=0;j<A.n;j++){
            y[i]=(y[i]+(x[j]*A.m[i][j])%MOD)%MOD;
        }
    }
    for (long long i=0;i<A.n;i++)
        x[i]=y[i];
}
long long sum;
long long w,n;
long long h;
long long k,p,q;
int main(){
    int i,j;
    int d=0;
    while (scanf("%lld%lld%lld",&w,&h,&n),w||h||n){
        memset(ones.m,0,sizeof ones.m);
        ones.n=w;
        for (i=0;i<w;i++)
            ones.m[i][i]=1;
        for (i=0;i<n;i++){
            scanf("%lld%lld",&Pos[i].a,&Pos[i].b);
            //Pos[i].a--;Pos[i].b--;
        }
        sort(Pos,Pos+n,cmp);

        memset(x,0,sizeof x);
        x[0]=1;
        memset(A.m,0,sizeof A.m);
        A.n=w;
        A.m[0][0]=1; A.m[0][1]=1;
        for (i=1;i<w;i++){
            A.m[i][i-1]=1;
            A.m[i][i]=1;
            A.m[i][i+1]=1;
        }
        //for (i=0;i<w;i++)
        //    x[i]=0;//列向量
        k=0;
        j=0;

        // ****************************************
        /*
        int i,j;
    int d=0;
    while (scanf("%lld%lld%lld",&w,&h,&n),w||h||n){
        memset(ones.m,0,sizeof ones.m);
        for (i=0;i<w;i++)
            ones.m[i][i]=1;
        ones.n=w;
        for (i=0;i<n;i++)
            scanf("%lld%lld",&Pos[i].a,&Pos[i].b);
        sort(Pos,Pos+n,cmp);
        //if ((w==0)&&(h==0)&&(n==0)) return 0;
        memset(x,0,sizeof x);
        x[0]=1;
        memset(A.m,0,sizeof A.m);
        A.n=w;
        A.m[0][0]=1;
        A.m[0][1]=1;
        for (i=1;i<w;i++){
            A.m[i][i]=1;
            A.m[i][i-1]=1;
            A.m[i][i+1]=1;
        }
        k=0;
        j=0;
        */
        //****************************************
        //printf("!!!");
        while (j<n){
            p=Pos[j].b-1;
            if (k==p){
                x[Pos[j].a-1]=0;
            }
            else{
                get_x(Pow(A,p-k));
                k=p;
                x[Pos[j].a-1]=0;
            }
            //printf("%d",k);
            //for (int i=0;i<w;i++)
            //    printf("%5d",x[i]);
            //printf("\n");
            j++;

        }
        //printf("!!!");
        if (k<h-1){

//            B=Pow(A,h-1-k);printf("#!!!");
            get_x(Pow(A,h-1-k));
        }
        printf("Case %lld: %lld\n",++d,(x[w-1]%MOD+MOD)%MOD);
    }
}
accuracy: 1
time_ms: 2980
memory_kb: 3592
final_rank: 4

code_snippet: #include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <cstring>
using namespace std;
#define MOD 1000000009
struct Matrix{
    long long n;

    Matrix(){
        memset(m,0,sizeof m);
    };
    long long m[80][80];
    Matrix operator * (const Matrix &b) const{
        Matrix c;
        for (long long i=0;i<n;i++)
        for (long long j=0;j<n;j++){
            c.m[i][j]=0;
            for (long long k=0;k<n;k++)
                c.m[i][j]=(c.m[i][j]+(m[i][k]*b.m[k][j])%MOD)%MOD;
        }
        c.n=n;
        return c;
    }
};
Matrix ones;
Matrix Pow(const Matrix &a,long long n){
    Matrix t=ones,y=a;
    while (n){
        if (n & 1) t = (t * y) ;
        y = (y * y) ;
        n /= 2;
    }
    return t;
}
struct node{
    long long a,b;
}Pos[80];
bool cmp(node a,node b){
    return a.b<b.b;
}
Matrix A,B;
long long x[80];
void get_x(Matrix A){
    long long y[80];
    //printf("!!!");
    memset(y,0,sizeof y);
    for (long long i=0;i<A.n;i++){
        for (long long j=0;j<A.n;j++){
            y[i]=(y[i]+(x[j]*A.m[i][j])%MOD)%MOD;
        }
    }
    for (long long i=0;i<A.n;i++)
        x[i]=y[i];
}
long long sum;
long long w,n;
long long h;
long long k,p,q;
int main(){
    long long i,j,now;
    long long d=0;
    while (scanf("%lld%lld%lld",&w,&h,&n),w||h||n){
        memset(ones.m,0,sizeof ones.m);
        ones.n=w;
        for (i=0;i<w;i++)
            ones.m[i][i]=1;
        for (i=0;i<n;i++){
            scanf("%lld%lld",&Pos[i].a,&Pos[i].b);
            //Pos[i].a--;Pos[i].b--;
        }
        sort(Pos,Pos+n,cmp);

        memset(x,0,sizeof x);
        x[0]=1;
        memset(A.m,0,sizeof A.m);
        A.n=w;
        A.m[0][0]=1; A.m[0][1]=1;
        for (i=1;i<w;i++){
            A.m[i][i-1]=1;
            A.m[i][i]=1;
            A.m[i][i+1]=1;
        }
        //for (i=0;i<w;i++)
        //    x[i]=0;//列向量
        k=0;
        j=0;

        // ****************************************
        /*
        int i,j;
    int d=0;
    while (scanf("%lld%lld%lld",&w,&h,&n),w||h||n){
        memset(ones.m,0,sizeof ones.m);
        for (i=0;i<w;i++)
            ones.m[i][i]=1;
        ones.n=w;
        for (i=0;i<n;i++)
            scanf("%lld%lld",&Pos[i].a,&Pos[i].b);
        sort(Pos,Pos+n,cmp);
        //if ((w==0)&&(h==0)&&(n==0)) return 0;
        memset(x,0,sizeof x);
        x[0]=1;
        memset(A.m,0,sizeof A.m);
        A.n=w;
        A.m[0][0]=1;
        A.m[0][1]=1;
        for (i=1;i<w;i++){
            A.m[i][i]=1;
            A.m[i][i-1]=1;
            A.m[i][i+1]=1;
        }
        k=0;
        j=0;
        */
        //****************************************
        //printf("!!!");
        while (j<n){
            p=Pos[j].b-1;
            if (k==p){
                x[Pos[j].a-1]=0;
            }
            else{
                get_x(Pow(A,p-k));
                k=p;
                x[Pos[j].a-1]=0;
            }
            //printf("%d",k);
            //for (int i=0;i<w;i++)
            //    printf("%5d",x[i]);
            //printf("\n");
            j++;

        }
        //printf("!!!");
        if (k<h-1){

//            B=Pow(A,h-1-k);printf("#!!!");
            get_x(Pow(A,h-1-k));
        }
        printf("Case %lld: %lld\n",++d,(x[w-1]%MOD+MOD)%MOD);
    }
}
accuracy: 1
time_ms: 2980
memory_kb: 3916
final_rank: 16

code_snippet: #include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <cstring>
using namespace std;
#define MOD 1000000009
struct Matrix{
    long long n;

    Matrix(){
        memset(m,0,sizeof m);
    };
    long long m[80][80];
    Matrix operator * (const Matrix &b) const{
        Matrix c;
        for (long long i=0;i<n;i++)
        for (long long j=0;j<n;j++){
            c.m[i][j]=0;
            for (long long k=0;k<n;k++)
                c.m[i][j]=(c.m[i][j]+(m[i][k]*b.m[k][j])%MOD)%MOD;
        }
        c.n=n;
        return c;
    }
};
Matrix ones;
Matrix Pow(const Matrix &a,long long n){
    Matrix t=ones,y=a;
    while (n){
        if (n & 1) t = (t * y) ;
        y = (y * y) ;
        n /= 2;
    }
    return t;
}
struct node{
    long long a,b;
}Pos[80];
bool cmp(node a,node b){
    return a.b<b.b;
}
Matrix A,B;
long long x[80];
void get_x(Matrix A){
    long long y[80];
    //printf("!!!");
    memset(y,0,sizeof y);
    for (long long i=0;i<A.n;i++){
        for (long long j=0;j<A.n;j++){
            y[i]=(y[i]+(x[j]*A.m[i][j])%MOD)%MOD;
        }
    }
    for (long long i=0;i<A.n;i++)
        x[i]=y[i];
}
long long sum;
long long w,n;
long long h;
long long k,p,q;
int main(){
    long long i,j,now;
    long long d=0;
    while (scanf("%lld%lld%lld",&w,&h,&n),w||h||n){
        /*
        memset(ones.m,0,sizeof ones.m);
        ones.n=w;
        for (i=0;i<w;i++)
            ones.m[i][i]=1;
        for (i=0;i<n;i++){
            scanf("%lld%lld",&Pos[i].a,&Pos[i].b);
            //Pos[i].a--;Pos[i].b--;
        }
        */
        memset(ones.m,0,sizeof ones.m);
        for (i=0;i<w;i++)
            ones.m[i][i]=1;
        ones.n=w;
        for (i=0;i<n;i++)
            scanf("%lld%lld",&Pos[i].a,&Pos[i].b);
        sort(Pos,Pos+n,cmp);
        memset(A.m,0,sizeof A.m);
        A.n=w;
        A.m[0][0]=1; A.m[0][1]=1;
        for (i=1;i<w;i++){
            A.m[i][i-1]=1;
            A.m[i][i]=1;
            A.m[i][i+1]=1;
        }
        //for (i=0;i<w;i++)
        //    x[i]=0;//列向量
        memset(x,0,sizeof x);
        x[0]=1;
        k=0;
        j=0;

        // ****************************************
        /*
        int i,j;
    int d=0;
    while (scanf("%lld%lld%lld",&w,&h,&n),w||h||n){
        memset(ones.m,0,sizeof ones.m);
        for (i=0;i<w;i++)
            ones.m[i][i]=1;
        ones.n=w;
        for (i=0;i<n;i++)
            scanf("%lld%lld",&Pos[i].a,&Pos[i].b);
        sort(Pos,Pos+n,cmp);
        //if ((w==0)&&(h==0)&&(n==0)) return 0;
        memset(x,0,sizeof x);
        x[0]=1;
        memset(A.m,0,sizeof A.m);
        A.n=w;
        A.m[0][0]=1;
        A.m[0][1]=1;
        for (i=1;i<w;i++){
            A.m[i][i]=1;
            A.m[i][i-1]=1;
            A.m[i][i+1]=1;
        }
        k=0;
        j=0;
        */
        //****************************************
        //printf("!!!");
        while (j<n){
            p=Pos[j].b-1;
            if (k==p){
                x[Pos[j].a-1]=0;
            }
            else{
                get_x(Pow(A,p-k));
                k=p;
                x[Pos[j].a-1]=0;
            }
            //printf("%d",k);
            //for (int i=0;i<w;i++)
            //    printf("%5d",x[i]);
            //printf("\n");
            j++;

        }
        //printf("!!!");
        if (k<h-1){

//            B=Pow(A,h-1-k);printf("#!!!");
            get_x(Pow(A,h-1-k));
        }
        printf("Case %lld: %lld\n",++d,(x[w-1]%MOD+MOD)%MOD);
    }
}
accuracy: 1
time_ms: 2990
memory_kb: 3744
final_rank: 6

code_snippet: #include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <cstring>
using namespace std;
#define MOD 1000000009
struct Matrix{
    long long n;

    Matrix(){
        memset(m,0,sizeof m);
    };
    long long m[80][80];
    Matrix operator * (const Matrix &b) const{
        Matrix c;
        for (long long i=0;i<n;i++)
        for (long long j=0;j<n;j++){
            c.m[i][j]=0;
            for (long long k=0;k<n;k++)
                c.m[i][j]=(c.m[i][j]+(m[i][k]*b.m[k][j])%MOD)%MOD;
        }
        c.n=n;
        return c;
    }
};
Matrix ones;
Matrix Pow(const Matrix &a,long long n){
    Matrix t=ones,y=a;
    while (n){
        if (n & 1) t = (t * y) ;
        y = (y * y) ;
        n /= 2;
    }
    return t;
}
struct node{
    long long a,b;
}Pos[80];
bool cmp(node a,node b){
    return a.b<b.b;
}
Matrix A,B;
long long x[80];
void get_x(Matrix A){
    long long y[80];
    //printf("!!!");
    memset(y,0,sizeof y);
    for (long long i=0;i<A.n;i++){
        for (long long j=0;j<A.n;j++){
            y[i]=(y[i]+(x[j]*A.m[i][j])%MOD)%MOD;
        }
    }
    for (long long i=0;i<A.n;i++)
        x[i]=y[i];
}
long long sum;
long long w,n;
long long h;
long long k,p,q;
int main(){
    long long i,j,now;
    long long d=0;
    while (scanf("%lld%lld%lld",&w,&h,&n),w||h||n){
        memset(ones.m,0,sizeof ones.m);
        ones.n=w;
        for (i=0;i<w;i++)
            ones.m[i][i]=1;
        for (i=0;i<n;i++){
            scanf("%lld%lld",&Pos[i].a,&Pos[i].b);
            //Pos[i].a--;Pos[i].b--;
        }
        sort(Pos,Pos+n,cmp);
        memset(A.m,0,sizeof A.m);
        A.n=w;
        A.m[0][0]=1; A.m[0][1]=1;
        for (i=1;i<w;i++){
            A.m[i][i-1]=1;
            A.m[i][i]=1;
            A.m[i][i+1]=1;
        }
        //for (i=0;i<w;i++)
        //    x[i]=0;//列向量
        memset(x,0,sizeof x);
        x[0]=1;
        k=0;
        j=0;

        // ****************************************
        /*
        int i,j;
    int d=0;
    while (scanf("%lld%lld%lld",&w,&h,&n),w||h||n){
        memset(ones.m,0,sizeof ones.m);
        for (i=0;i<w;i++)
            ones.m[i][i]=1;
        ones.n=w;
        for (i=0;i<n;i++)
            scanf("%lld%lld",&Pos[i].a,&Pos[i].b);
        sort(Pos,Pos+n,cmp);
        //if ((w==0)&&(h==0)&&(n==0)) return 0;
        memset(x,0,sizeof x);
        x[0]=1;
        memset(A.m,0,sizeof A.m);
        A.n=w;
        A.m[0][0]=1;
        A.m[0][1]=1;
        for (i=1;i<w;i++){
            A.m[i][i]=1;
            A.m[i][i-1]=1;
            A.m[i][i+1]=1;
        }
        k=0;
        j=0;
        */
        //****************************************
        //printf("!!!");
        while (j<n){
            p=Pos[j].b-1;
            if (k==p){
                x[Pos[j].a-1]=0;
            }
            else{
                get_x(Pow(A,p-k));
                k=p;
                x[Pos[j].a-1]=0;
            }
            //printf("%d",k);
            //for (int i=0;i<w;i++)
            //    printf("%5d",x[i]);
            //printf("\n");
            j++;

        }
        //printf("!!!");
        if (k<h-1){

//            B=Pow(A,h-1-k);printf("#!!!");
            get_x(Pow(A,h-1-k));
        }
        printf("Case %lld: %lld\n",++d,(x[w-1]%MOD+MOD)%MOD);
    }
}
accuracy: 1
time_ms: 2990
memory_kb: 3844
final_rank: 11

code_snippet: #include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <cstring>
using namespace std;
#define MOD 1000000009
struct Matrix{
    long long n;

    Matrix(){
        memset(m,0,sizeof m);
    };
    long long m[80][80];
    Matrix operator * (const Matrix &b) const{
        Matrix c;
        for (long long i=0;i<n;i++)
        for (long long j=0;j<n;j++){
            c.m[i][j]=0;
            for (long long k=0;k<n;k++)
                c.m[i][j]=(c.m[i][j]+(m[i][k]*b.m[k][j])%MOD)%MOD;
        }
        c.n=n;
        return c;
    }
};
Matrix ones;
Matrix Pow(const Matrix &a,long long n){
    Matrix t=ones,y=a;
    while (n){
        if (n & 1) t = (t * y) ;
        y = (y * y) ;
        n /= 2;
    }
    return t;
}
struct node{
    long long a,b;
}Pos[80];
bool cmp(node a,node b){
    return a.b<b.b;
}
Matrix A,B;
long long x[80];
void get_x(Matrix A){
    long long y[80];
    //printf("!!!");
    memset(y,0,sizeof y);
    for (long long i=0;i<A.n;i++){
        for (long long j=0;j<A.n;j++){
            y[i]=(y[i]+(x[j]*A.m[i][j])%MOD)%MOD;
        }
    }
    for (long long i=0;i<A.n;i++)
        x[i]=y[i];
}
long long sum;
long long w,n;
long long h;
long long k,p,q;
int main(){
    long long i,j,now;
    long long d=0;
    while (scanf("%lld%lld%lld",&w,&h,&n),w||h||n){
        memset(ones.m,0,sizeof ones.m);
        ones.n=w;
        for (i=0;i<w;i++)
            ones.m[i][i]=1;
        for (i=0;i<n;i++){
            scanf("%lld%lld",&Pos[i].a,&Pos[i].b);
            //Pos[i].a--;Pos[i].b--;
        }
        sort(Pos,Pos+n,cmp);
        memset(A.m,0,sizeof A.m);
        A.n=w;
        A.m[0][0]=1; A.m[0][1]=1;
        for (i=1;i<w;i++){
            A.m[i][i-1]=1;
            A.m[i][i]=1;
            A.m[i][i+1]=1;
        }
        for (i=0;i<w;i++)
            x[i]=0;//列向量
        x[0]=1;
        k=0;
        j=0;

        // ****************************************
        /*
        int i,j;
    int d=0;
    while (scanf("%lld%lld%lld",&w,&h,&n),w||h||n){
        memset(ones.m,0,sizeof ones.m);
        for (i=0;i<w;i++)
            ones.m[i][i]=1;
        ones.n=w;
        for (i=0;i<n;i++)
            scanf("%lld%lld",&Pos[i].a,&Pos[i].b);
        sort(Pos,Pos+n,cmp);
        //if ((w==0)&&(h==0)&&(n==0)) return 0;
        memset(x,0,sizeof x);
        x[0]=1;
        memset(A.m,0,sizeof A.m);
        A.n=w;
        A.m[0][0]=1;
        A.m[0][1]=1;
        for (i=1;i<w;i++){
            A.m[i][i]=1;
            A.m[i][i-1]=1;
            A.m[i][i+1]=1;
        }
        k=0;
        j=0;
        */
        //****************************************
        //printf("!!!");
        while (j<n){
            p=Pos[j].b-1;
            if (k==p){
                x[Pos[j].a-1]=0;
            }
            else{
                get_x(Pow(A,p-k));
                k=p;
                x[Pos[j].a-1]=0;
            }
            //printf("%d",k);
            //for (int i=0;i<w;i++)
            //    printf("%5d",x[i]);
            //printf("\n");
            j++;

        }
        //printf("!!!");
        if (k<h-1){

//            B=Pow(A,h-1-k);printf("#!!!");
            get_x(Pow(A,h-1-k));
        }
        printf("Case %lld: %lld\n",++d,(x[w-1]%MOD+MOD)%MOD);
    }
}
accuracy: 1
time_ms: 2990
memory_kb: 3800
final_rank: 7

code_snippet: #include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <cstring>
using namespace std;
#define MOD 1000000009
struct Matrix{
    long long n;

    Matrix(){
        memset(m,0,sizeof m);
    };
    long long m[80][80];
    Matrix operator * (const Matrix &b) const{
        Matrix c;
        for (long long i=0;i<n;i++)
        for (long long j=0;j<n;j++){
            c.m[i][j]=0;
            for (long long k=0;k<n;k++)
                c.m[i][j]=(c.m[i][j]+(m[i][k]*b.m[k][j])%MOD)%MOD;
        }
        c.n=n;
        return c;
    }
};
Matrix ones;
Matrix Pow(const Matrix &a,long long n){
    Matrix t=ones,y=a;
    while (n){
        if (n & 1) t = (t * y) ;
        y = (y * y) ;
        n /= 2;
    }
    return t;
}
struct node{
    long long a,b;
}Pos[80];
bool cmp(node a,node b){
    return a.b<b.b;
}
Matrix A,B;
long long x[80];
void get_x(Matrix A){
    long long y[80];
    //printf("!!!");
    memset(y,0,sizeof y);
    for (long long i=0;i<A.n;i++){
        for (long long j=0;j<A.n;j++){
            y[i]=(y[i]+(x[j]*A.m[i][j])%MOD)%MOD;
        }
    }
    for (long long i=0;i<A.n;i++)
        x[i]=y[i];
}
long long sum;
long long w,n;
long long h;
long long k,p,q;
int main(){
    long long i,j,now;
    long long d=0;
    while (scanf("%lld%lld%lld",&w,&h,&n),w||h||n){
        memset(ones.m,0,sizeof ones.m);
        ones.n=w;
        for (i=0;i<w;i++)
            ones.m[i][i]=1;
        for (i=0;i<n;i++){
            scanf("%lld%lld",&Pos[i].a,&Pos[i].b);
            //Pos[i].a--;Pos[i].b--;
        }
        sort(Pos,Pos+n,cmp);
        memset(A.m,0,sizeof A.m);
        A.n=w;
        A.m[0][0]=1; A.m[0][1]=1;
        for (i=1;i<w;i++){
            A.m[i][i-1]=1;
            A.m[i][i]=1;
            A.m[i][i+1]=1;
        }
        for (i=0;i<w;i++)
            x[i]=0;//列向量
        x[0]=1;
        k=0;
        j=0;
        //printf("!!!");
        while (j<n){
            p=Pos[j].b-1;
            if (k==p){
                x[Pos[j].a-1]=0;
            }
            else{
                get_x(Pow(A,p-k));
                k=p;
                x[Pos[j].a-1]=0;
            }
            //printf("%d",k);
            //for (int i=0;i<w;i++)
            //    printf("%5d",x[i]);
            //printf("\n");
            j++;

        }
        //printf("!!!");
        if (k<h-1){

//            B=Pow(A,h-1-k);printf("#!!!");
            get_x(Pow(A,h-1-k));
        }
        printf("Case %lld: %lld\n",++d,(x[w-1]%MOD+MOD)%MOD);
    }
}
accuracy: 1
time_ms: 2990
memory_kb: 3844
final_rank: 11

code_snippet: #include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <cstring>
using namespace std;
#define MOD 1000000009
struct Matrix{
    long long n;

    Matrix(){
        memset(m,0,sizeof m);
    };
    long long m[80][80];
    Matrix operator * (const Matrix &b) const{
        Matrix c;
        for (long long i=0;i<n;i++)
        for (long long j=0;j<n;j++){
            c.m[i][j]=0;
            for (long long k=0;k<n;k++)
                c.m[i][j]=(c.m[i][j]+(m[i][k]*b.m[k][j])%MOD)%MOD;
        }
        c.n=n;
        return c;
    }
};
Matrix ones;
Matrix Pow(const Matrix &a,long long n){
    Matrix t=ones,y=a;
    while (n){
        if (n & 1) t = (t * y) ;
        y = (y * y) ;
        n /= 2;
    }
    return t;
}
struct node{
    long long a,b;
}Pos[80];
bool cmp(node a,node b){
    return a.b<b.b;
}
Matrix A,B;
long long x[80];
void get_x(Matrix A){
    long long y[80];
    //printf("!!!");
    memset(y,0,sizeof y);
    for (long long i=0;i<A.n;i++){
        for (long long j=0;j<A.n;j++){
            y[i]=(y[i]+(x[j]*A.m[i][j])%MOD)%MOD;
        }
    }
    for (long long i=0;i<A.n;i++)
        x[i]=y[i];
}
long long sum;
long long w,n;
long long h;
int main(){
    long long i,j,now;
    long long k;
    long long p,q;
    long long d=0;
    while (scanf("%lld%lld%lld",&w,&h,&n),w||h||n){
        memset(ones.m,0,sizeof ones.m);
        ones.n=w;
        for (i=0;i<w;i++)
            ones.m[i][i]=1;
        for (i=0;i<n;i++){
            scanf("%lld%lld",&Pos[i].a,&Pos[i].b);
            //Pos[i].a--;Pos[i].b--;
        }
        sort(Pos,Pos+n,cmp);
        memset(A.m,0,sizeof A.m);
        A.n=w;
        A.m[0][0]=1; A.m[0][1]=1;
        for (i=1;i<w;i++){
            A.m[i][i-1]=1;
            A.m[i][i]=1;
            A.m[i][i+1]=1;
        }
        for (i=0;i<w;i++)
            x[i]=0;//列向量
        x[0]=1;
        k=0;
        j=0;
        //printf("!!!");
        while (j<n){
            p=Pos[j].b-1;
            if (k==p){
                x[Pos[j].a-1]=0;
            }
            else{
                get_x(Pow(A,p-k));
                k=p;
                x[Pos[j].a-1]=0;
            }
            //printf("%d",k);
            //for (int i=0;i<w;i++)
            //    printf("%5d",x[i]);
            //printf("\n");
            j++;

        }
        //printf("!!!");
        if (k<h-1){

//            B=Pow(A,h-1-k);printf("#!!!");
            get_x(Pow(A,h-1-k));
        }
        printf("Case %lld: %lld\n",++d,(x[w-1]%MOD+MOD)%MOD);
    }
}
accuracy: 1
time_ms: 2960
memory_kb: 3800
final_rank: 5

code_snippet: #include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <cstring>
using namespace std;
#define MOD 1000000009
struct Matrix{
    long long n;

    Matrix(){
        memset(m,0,sizeof m);
    };
    long long m[80][80];
    Matrix operator * (const Matrix &b) const{
        Matrix c;
        for (long long i=0;i<n;i++)
        for (long long j=0;j<n;j++){
            c.m[i][j]=0;
            for (long long k=0;k<n;k++)
                c.m[i][j]=(c.m[i][j]+(m[i][k]*b.m[k][j])%MOD)%MOD;
        }
        c.n=n;
        return c;
    }
};
Matrix ones;
Matrix Pow(const Matrix &a,unsigned long long n){
    Matrix t=ones,y=a;
    while (n){
        if (n & 1) t = (t * y) ;
        y = (y * y) ;
        n /= 2;
    }
    return t;
}
struct node{
    unsigned long long a,b;
}Pos[80];
bool cmp(node a,node b){
    return a.b<b.b;
}
Matrix A,B;
unsigned long long x[80];
void get_x(Matrix A){
    unsigned long long y[80];
    //printf("!!!");
    memset(y,0,sizeof y);
    for (unsigned long long i=0;i<A.n;i++){
        for (unsigned long long j=0;j<A.n;j++){
            y[i]=(y[i]+(x[j]*A.m[i][j])%MOD)%MOD;
        }
    }
    for (unsigned long long i=0;i<A.n;i++)
        x[i]=y[i];
}
unsigned long long sum;
unsigned long long w,n;
unsigned long long h;
int main(){
    unsigned long long i,j,now;
    unsigned long long k;
    unsigned long long p,q;
    unsigned long long d=0;
    while (scanf("%llu%llu%llu",&w,&h,&n),w||h||n){
        memset(ones.m,0,sizeof ones.m);
        ones.n=w;
        for (i=0;i<w;i++)
            ones.m[i][i]=1;
        for (i=0;i<n;i++){
            scanf("%llu%llu",&Pos[i].a,&Pos[i].b);
            //Pos[i].a--;Pos[i].b--;
        }
        sort(Pos,Pos+n,cmp);
        memset(A.m,0,sizeof A.m);
        A.n=w;
        A.m[0][0]=1; A.m[0][1]=1;
        for (i=1;i<w;i++){
            A.m[i][i-1]=1;
            A.m[i][i]=1;
            A.m[i][i+1]=1;
        }
        for (i=0;i<w;i++)
            x[i]=0;//列向量
        x[0]=1;
        k=0;
        j=0;
        //printf("!!!");
        while (j<n){
            p=Pos[j].b-1;
            if (k==p){
                x[Pos[j].a-1]=0;
            }
            else{
                get_x(Pow(A,p-k));
                k=p;
                x[Pos[j].a-1]=0;
            }
            //printf("%d",k);
            //for (int i=0;i<w;i++)
            //    printf("%5d",x[i]);
            //printf("\n");
            j++;

        }
        //printf("!!!");
        if (k<h-1){

//            B=Pow(A,h-1-k);printf("#!!!");
            get_x(Pow(A,h-1-k));
        }
        printf("Case %llu: %llu\n",++d,(x[w-1]%MOD+MOD)%MOD);
    }
}
accuracy: 1
time_ms: 2980
memory_kb: 3880
final_rank: 13

code_snippet: #include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <cstring>
using namespace std;
#define MOD 1000000009
struct Matrix{
    long long n;

    Matrix(){
        memset(m,0,sizeof m);
    };
    long long m[80][80];
    Matrix operator * (const Matrix &b) const{
        Matrix c;
        for (long long i=0;i<n;i++)
        for (long long j=0;j<n;j++){
            c.m[i][j]=0;
            for (long long k=0;k<n;k++)
                c.m[i][j]=(c.m[i][j]+(m[i][k]*b.m[k][j])%MOD)%MOD;
        }
        c.n=n;
        return c;
    }
};
Matrix ones;
Matrix Pow(const Matrix &a,unsigned long long n){
    Matrix t=ones,y=a;
    while (n){
        if (n & 1) t = (t * y) ;
        y = (y * y) ;
        n /= 2;
    }
    return t;
}
struct node{
    unsigned long long a,b;
}Pos[80];
bool cmp(node a,node b){
    return a.b<b.b;
}
Matrix A,B;
unsigned long long x[80];
void get_x(Matrix A){
    unsigned long long y[80];
    //printf("!!!");
    memset(y,0,sizeof y);
    for (unsigned long long i=0;i<A.n;i++){
        for (unsigned long long j=0;j<A.n;j++){
            y[i]=(y[i]+(x[j]*A.m[i][j])%MOD)%MOD;
        }
    }
    for (unsigned long long i=0;i<A.n;i++)
        x[i]=y[i];
}
unsigned long long sum;
unsigned long long w,n;
unsigned long long h;
int main(){
    unsigned long long i,j,now;
    unsigned long long k;
    unsigned long long p,q;
    unsigned long long d=0;
    while (scanf("%llu%llu%llu",&w,&h,&n),w||h||n){
        memset(ones.m,0,sizeof ones.m);
        ones.n=w;
        for (i=0;i<w;i++)
            ones.m[i][i]=1;
        for (i=0;i<n;i++){
            scanf("%llu%llu",&Pos[i].a,&Pos[i].b);
            Pos[i].a--;Pos[i].b--;
        }
        sort(Pos,Pos+n,cmp);
        memset(A.m,0,sizeof A.m);
        A.n=w;
        A.m[0][0]=1; A.m[0][1]=1;
        for (i=1;i<w;i++){
            A.m[i][i-1]=1;
            A.m[i][i]=1;
            A.m[i][i+1]=1;
        }
        for (i=0;i<w;i++)
            x[i]=0;//列向量
        x[0]=1;
        k=0;
        j=0;
        //printf("!!!");
        while (j<n){
            p=Pos[j].b;
            if (k==p){
                x[Pos[j].a]=0;
            }
            else{
                get_x(Pow(A,p-k));
                k=p;
                x[Pos[j].a]=0;
            }
            //printf("%d",k);
            //for (int i=0;i<w;i++)
            //    printf("%5d",x[i]);
            //printf("\n");
            j++;

        }
        //printf("!!!");
        if (k<h-1){

//            B=Pow(A,h-1-k);printf("#!!!");
            get_x(Pow(A,h-1-k));
        }
        printf("Case %llu: %llu\n",++d,(x[w-1]%MOD+MOD)%MOD);
    }
}
accuracy: 1
time_ms: 2980
memory_kb: 3892
final_rank: 14

code_snippet: #include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <cstring>
using namespace std;
#define MOD 1000000009
struct Matrix{
    long long n;

    Matrix(){
        memset(m,0,sizeof m);
    };
    long long m[80][80];
    Matrix operator * (const Matrix &b) const{
        Matrix c;
        for (long long i=0;i<n;i++)
        for (long long j=0;j<n;j++){
            c.m[i][j]=0;
            for (long long k=0;k<n;k++)
                c.m[i][j]=(c.m[i][j]+(m[i][k]*b.m[k][j])%MOD)%MOD;
        }
        c.n=n;
        return c;
    }
};
Matrix ones;
Matrix Pow(const Matrix &a,unsigned long long n){
    Matrix t=ones,y=a;
    while (n){
        if (n & 1) t = (t * y) ;
        y = (y * y) ;
        n /= 2;
    }
    return t;
}
struct node{
    unsigned long long a,b;
}Pos[80];
bool cmp(node a,node b){
    return a.b<b.b;
}
Matrix A,B;
unsigned long long x[80];
void get_x(Matrix A){
    unsigned long long y[80];
    //printf("!!!");
    memset(y,0,sizeof y);
    for (unsigned long long i=0;i<A.n;i++){
        for (unsigned long long j=0;j<A.n;j++){
            y[i]=(y[i]+(x[j]*A.m[i][j])%MOD)%MOD;
        }
    }
    for (unsigned long long i=0;i<A.n;i++)
        x[i]=y[i];
}
unsigned long long sum;
unsigned long long w,n;
unsigned long long h;
int main(){
    unsigned long long i,j,now;
    unsigned long long k;
    unsigned long long p,q;
    unsigned long long d=0;
    while (scanf("%llu%llu%llu",&w,&h,&n)!=EOF){
        if (w==0) return 0;
        memset(ones.m,0,sizeof ones.m);
        ones.n=w;
        for (i=0;i<w;i++)
            ones.m[i][i]=1;
        for (i=0;i<n;i++){
            scanf("%llu%llu",&Pos[i].a,&Pos[i].b);
            Pos[i].a--;Pos[i].b--;
        }
        sort(Pos,Pos+n,cmp);
        memset(A.m,0,sizeof A.m);
        A.n=w;
        A.m[0][0]=1; A.m[0][1]=1;
        for (i=1;i<w;i++){
            A.m[i][i-1]=1;
            A.m[i][i]=1;
            A.m[i][i+1]=1;
        }
        for (i=0;i<w;i++)
            x[i]=0;//列向量
        x[0]=1;
        k=0;
        j=0;
        //printf("!!!");
        while (j<n){
            p=Pos[j].b;
            if (k==p){
                x[Pos[j].a]=0;
            }
            else{
                get_x(Pow(A,p-k));
                k=p;
                x[Pos[j].a]=0;
            }
            //printf("%d",k);
            //for (int i=0;i<w;i++)
            //    printf("%5d",x[i]);
            //printf("\n");
            j++;

        }
        //printf("!!!");
        if (k<h-1){

//            B=Pow(A,h-1-k);printf("#!!!");
            get_x(Pow(A,h-1-k));
        }
        printf("Case %llu: %llu\n",++d,(x[w-1]%MOD+MOD)%MOD);
    }
}
accuracy: 1
time_ms: 2990
memory_kb: 3916
final_rank: 19

code_snippet: #include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <cstring>
using namespace std;
#define MOD 1000000009
struct Matrix{
    long long n;

    Matrix(){
        memset(m,0,sizeof m);
    };
    long long m[80][80];
    Matrix operator * (const Matrix &b) const{
        Matrix c;
        for (long long i=0;i<n;i++)
        for (long long j=0;j<n;j++){
            c.m[i][j]=0;
            for (long long k=0;k<n;k++)
                c.m[i][j]=(c.m[i][j]+(m[i][k]*b.m[k][j])%MOD)%MOD;
        }
        c.n=n;
        return c;
    }
};
Matrix ones;
Matrix Pow(const Matrix &a,unsigned long long n){
    Matrix t=ones,y=a;
    while (n){
        if (n & 1) t = (t * y) ;
        y = (y * y) ;
        n /= 2;
    }
    return t;
}
struct node{
    unsigned long long a,b;
}Pos[50];
bool cmp(node a,node b){
    return a.b<b.b;
}
Matrix A,B;
unsigned long long x[80];
void get_x(Matrix A){
    unsigned long long y[80];
    //printf("!!!");
    memset(y,0,sizeof y);
    for (unsigned long long i=0;i<A.n;i++){
        for (unsigned long long j=0;j<A.n;j++){
            y[i]=(y[i]+(x[j]*A.m[i][j])%MOD)%MOD;
        }
    }
    for (unsigned long long i=0;i<A.n;i++)
        x[i]=y[i];
}
unsigned long long sum;
unsigned long long w,n;
unsigned long long h;
int main(){
    unsigned long long i,j,now;
    unsigned long long k;
    unsigned long long p,q;
    unsigned long long d=0;
    while (scanf("%llu%llu%llu",&w,&h,&n)!=EOF){
        if (w==0) return 0;
        memset(ones.m,0,sizeof ones.m);
        ones.n=w;
        for (i=0;i<w;i++)
            ones.m[i][i]=1;
        for (i=0;i<n;i++){
            scanf("%llu%llu",&Pos[i].a,&Pos[i].b);
            Pos[i].a--;Pos[i].b--;
        }
        sort(Pos,Pos+n,cmp);
        memset(A.m,0,sizeof A.m);
        A.n=w;
        A.m[0][0]=1; A.m[0][1]=1;
        for (i=1;i<w;i++){
            A.m[i][i-1]=1;
            A.m[i][i]=1;
            A.m[i][i+1]=1;
        }
        for (i=0;i<w;i++)
            x[i]=0;//列向量
        x[0]=1;
        k=0;
        j=0;
        //printf("!!!");
        while (j<n){
            p=Pos[j].b;
            if (k==p){
                x[Pos[j].a]=0;
            }
            else{
                get_x(Pow(A,p-k));
                k=p;
                x[Pos[j].a]=0;
            }
            //printf("%d",k);
            //for (int i=0;i<w;i++)
            //    printf("%5d",x[i]);
            //printf("\n");
            j++;

        }
        //printf("!!!");
        if (k<h-1){

//            B=Pow(A,h-1-k);printf("#!!!");
            get_x(Pow(A,h-1-k));
        }
        printf("Case %llu: %llu\n",++d,(x[w-1]%MOD+MOD)%MOD);
    }
}
accuracy: 1
time_ms: 2990
memory_kb: 3840
final_rank: 10

code_snippet: #include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <cstring>
using namespace std;
#define MOD 1000000009
struct Matrix{
    unsigned long long n;

    Matrix(){
        memset(m,0,sizeof m);
    };

    Matrix(const Matrix &ipt){
        n = ipt.n;
        for (unsigned long long i=0;i<n;++i){
            for (unsigned long long j=0;j<n;++j){
                m[i][j] = ipt.m[i][j];
            }
        }
    }
    unsigned long long m[80][80];
    Matrix operator * (const Matrix &b) const{
        Matrix c;
        for (unsigned long long i=0;i<n;i++)
        for (unsigned long long j=0;j<n;j++){
            c.m[i][j]=0;
            for (unsigned long long k=0;k<n;k++)
                c.m[i][j]=(c.m[i][j]+(m[i][k]*b.m[k][j])%MOD)%MOD;
        }
        c.n=n;
        return c;
    }
};
Matrix ones;
Matrix Pow(const Matrix &a,unsigned long long n){
    Matrix t=ones,y=a;
    while (n){
        if (n & 1) t = (t * y) ;
        y = (y * y) ;
        n /= 2;
    }
    return t;
}
struct node{
    unsigned long long a,b;
}Pos[50];
bool cmp(node a,node b){
    return a.b<b.b;
}
Matrix A,B;
unsigned long long x[80];
void get_x(Matrix A){
    unsigned long long y[80];
    //printf("!!!");
    memset(y,0,sizeof y);
    for (unsigned long long i=0;i<A.n;i++){
        for (unsigned long long j=0;j<A.n;j++){
            y[i]=(y[i]+(x[j]*A.m[i][j])%MOD)%MOD;
        }
    }
    for (unsigned long long i=0;i<A.n;i++)
        x[i]=y[i];
}
unsigned long long sum;
unsigned long long w,n;
unsigned long long h;
int main(){
    unsigned long long i,j,now;
    unsigned long long k;
    unsigned long long p,q;
    unsigned long long d=0;
    while (scanf("%llu%llu%llu",&w,&h,&n)!=EOF){
        if (w==0) return 0;
        memset(ones.m,0,sizeof ones.m);
        ones.n=w;
        for (i=0;i<w;i++)
            ones.m[i][i]=1;
        for (i=0;i<n;i++){
            scanf("%llu%llu",&Pos[i].a,&Pos[i].b);
            Pos[i].a--;Pos[i].b--;
        }
        sort(Pos,Pos+n,cmp);
        memset(A.m,0,sizeof A.m);
        A.n=w;
        A.m[0][0]=1; A.m[0][1]=1;
        for (i=1;i<w;i++){
            A.m[i][i-1]=1;
            A.m[i][i]=1;
            A.m[i][i+1]=1;
        }
        for (i=0;i<w;i++)
            x[i]=0;//列向量
        x[0]=1;
        k=0;
        j=0;
        //printf("!!!");
        while (j<n){
            p=Pos[j].b;
            if (k==p){
                x[Pos[j].a]=0;
            }
            else{
                get_x(Pow(A,p-k));
                k=p;
                x[Pos[j].a]=0;
            }
            //printf("%d",k);
            //for (int i=0;i<w;i++)
            //    printf("%5d",x[i]);
            //printf("\n");
            j++;

        }
        //printf("!!!");
        if (k<h-1){

//            B=Pow(A,h-1-k);printf("#!!!");
            get_x(Pow(A,h-1-k));
        }
        printf("Case %llu: %llu\n",++d,(x[w-1]%MOD+MOD)%MOD);
    }
}
accuracy: 1
time_ms: 2320
memory_kb: 3760
final_rank: 2

code_snippet: #include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <cstring>
using namespace std;
#define MOD 1000000009
struct Matrix{
    unsigned long long n;

    Matrix(){
        memset(m,0,sizeof m);
    };

    Matrix(const Matrix &ipt){
        n = ipt.n;
        for (unsigned long long i=0;i<n;++i){
            for (unsigned long long j=0;j<n;++j){
                m[i][j] = ipt.m[i][j];
            }
        }
    }
    unsigned long long m[80][80];
    Matrix operator * (const Matrix &b) const{
        Matrix c;
        for (unsigned long long i=0;i<n;i++)
        for (unsigned long long j=0;j<n;j++){
            c.m[i][j]=0;
            for (unsigned long long k=0;k<n;k++)
                c.m[i][j]=(c.m[i][j]+(m[i][k]*b.m[k][j])%MOD)%MOD;
        }
        c.n=n;
        return c;
    }
};
Matrix ones;
Matrix Pow(const Matrix &a,unsigned long long n){
    Matrix t=ones,y=a;
    while (n){
        if (n & 1) t = (t * y) ;
        y = (y * y) ;
        n /= 2;
    }
    return t;
}
struct node{
    unsigned long long a,b;
}Pos[50];
bool cmp(node a,node b){
    return a.b<b.b;
}
Matrix A,B;
unsigned long long x[80];
void get_x(Matrix A){
    unsigned long long y[80];
    //printf("!!!");
    memset(y,0,sizeof y);
    for (unsigned long long i=0;i<A.n;i++){
        for (unsigned long long j=0;j<A.n;j++){
            y[i]=(y[i]+(x[j]*A.m[i][j])%MOD)%MOD;
        }
    }
    for (unsigned long long i=0;i<A.n;i++)
        x[i]=y[i];
}
unsigned long long sum;
unsigned long long w,n;
unsigned long long h;
int main(){
    unsigned long long i,j,now;
    unsigned long long k;
    unsigned long long p,q;
    unsigned long long d=0;
    while (scanf("%llu%llu%llu",&w,&h,&n),w||h||n){
        memset(ones.m,0,sizeof ones.m);
        ones.n=w;
        for (i=0;i<w;i++)
            ones.m[i][i]=1;
        for (i=0;i<n;i++){
            scanf("%llu%llu",&Pos[i].a,&Pos[i].b);
            Pos[i].a--;Pos[i].b--;
        }
        sort(Pos,Pos+n,cmp);
        memset(A.m,0,sizeof A.m);
        A.n=w;
        A.m[0][0]=1; A.m[0][1]=1;
        for (i=1;i<w;i++){
            A.m[i][i-1]=1;A.m[i][i]=1;A.m[i][i+1]=1;
        }
        for (i=0;i<w;i++)
            x[i]=0;//列向量
        x[0]=1;
        k=0;
        j=0;
        //printf("!!!");
        while (j<n){
            p=Pos[j].b;
            if (k==p){
                x[Pos[j].a]=0;
            }
            else{
                get_x(Pow(A,p-k));
                k=p;
                x[Pos[j].a]=0;
            }
            //printf("%d",k);
            //for (int i=0;i<w;i++)
            //    printf("%5d",x[i]);
            //printf("\n");
            j++;

        }
        //printf("!!!");
        if (k<h-1){

//            B=Pow(A,h-1-k);printf("#!!!");
            get_x(Pow(A,h-1-k));
        }
        printf("Case %llu: %llu\n",++d,(x[w-1]%MOD+MOD)%MOD);
    }
}
accuracy: 1
time_ms: 2320
memory_kb: 3748
final_rank: 1