#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef complex<LD> CMP;
static const int MODVAL = 1234567891;
struct mint
{
unsigned val;
mint():val(0){}
mint(int x):val(x%MODVAL) {}
mint(unsigned x):val(x%MODVAL) {}
mint(LL x):val(x%MODVAL) {}
};
mint& operator+=(mint& x, mint y) { return x = x.val+y.val; }
mint& operator-=(mint& x, mint y) { return x = x.val-y.val+MODVAL; }
mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; }
mint operator+(mint x, mint y) { return x+=y; }
mint operator-(mint x, mint y) { return x-=y; }
mint operator*(mint x, mint y) { return x*=y; }
template<typename T>
vector<T> MATMUL(const vector< vector<T> >& a, const vector<T>& v)
{
int N = a.size();
vector<T> u(N);
for(int i=0; i<N; ++i)
for(int j=0; j<N; ++j)
u[i] += a[i][j]*v[j];
return u;
}
template<typename T>
vector< vector<T> > MATMUL(const vector< vector<T> >& a, const vector< vector<T> >& b)
{
int N = a.size();
vector< vector<T> > c(N, vector<T>(N));
for(int i=0; i<N; ++i)
for(int j=0; j<N; ++j)
for(int k=0; k<N; ++k)
c[i][j] += a[i][k]*b[k][j];
return c;
}
template<typename T>
vector< vector<T> > MATPOW(vector< vector<T> > a, LL e)
{
int N = a.size();
vector< vector<T> > c(N, vector<T>(N));
for(int i=0; i<N; ++i) c[i][i] = 1;
for(; e; e>>=1) {
if(e&1)
c = MATMUL(c, a);
a = MATMUL(a, a);
}
return c;
}
class TheBrickTowerHardDivOne { public:
int find(int C, int K, long long H)
{
vector< vector<int> > patterns;
{
vector<int> p;
vector<bool> used(4);
generate_patterns(true, 4, used, p, 0, patterns);
}
const vector<int> compress = calc_compress_table(patterns).first;
const vector<bool> is_repr = calc_compress_table(patterns).second;
const int PN = patterns.size();
const int CPN = *max_element(compress.begin(), compress.end())+1;
const int D = CPN * (K+1);
vector< vector<mint> > m(D+1, vector<mint>(D+1));
for(int pa=0; pa<PN; ++pa) if(is_repr[pa])
for(int pb=0; pb<PN; ++pb) {
vector<mint> tt = transition(patterns[pa], patterns[pb], K, C);
for(int ka=0; ka<=K; ++ka)
for(int kt=0; kt<=K; ++kt) {
int kb = ka + kt + internal(patterns[pb]);
if(kb <= K)
m[compress[pb]*(K+1)+kb][compress[pa]*(K+1)+ka] += tt[kt];
}
}
for(int x=0; x<D+1; ++x)
m[D][x] = 1;
vector<mint> v(D+1);
for(int pa=0; pa<PN; ++pa) {
int ka = internal(patterns[pa]);
if(ka <= K)
v[compress[pa]*(K+1)+ka] += counting(patterns[pa], C);
}
v = MATMUL(MATPOW(m, H-1), v);
return accumulate(v.begin(), v.end(), mint(0)).val;
}
// TODO: auto generate.
pair< vector<int>, vector<bool> >
calc_compress_table(const vector< vector<int> >& patterns)
{
// {0} 00,00
// {1,2,5,9} 00,01
// {3,6} 00,11
// {4,7,12,13} 00,12
// {8} 01,10
// {10,11} 01,12
// {14} 01,23
vector<int> repr(patterns.size());
vector<bool> is_repr(patterns.size(), false);
is_repr[0] = true; repr[0] = 0;
is_repr[1] = true; repr[1] = repr[2] = repr[5] = repr[9] = 1;
is_repr[3] = true; repr[3] = repr[6] = 2;
is_repr[4] = true; repr[4] = repr[7] = repr[12] = repr[13] = 3;
is_repr[8] = true; repr[8] = 4;
is_repr[10] = true; repr[10] = repr[11] = 5;
is_repr[14] = true; repr[14] = 6;
return make_pair(repr, is_repr);
}
mint counting(const vector<int>& p, int C)
{
return counting(set<int>(p.begin(), p.end()).size(), C);
}
mint counting(int N, int C)
{
if(C<0)
return 0;
mint v = 1;
for(int i=0; i<N; ++i)
v *= (C-i);
return v;
}
int internal(const vector<int>& p)
{
return (p[0]==p[1])+(p[1]==p[3])+(p[3]==p[2])+(p[2]==p[0]);
}
vector<mint> transition(const vector<int>& pa, const vector<int>& pb, int K, int C)
{
const int AN = set<int>(pa.begin(), pa.end()).size();
const int BN = set<int>(pb.begin(), pb.end()).size();
vector< vector<int> > patterns;
{
vector<int> p;
vector<bool> used(AN+BN, false);
generate_patterns(false, BN, used, p, AN, patterns);
}
vector<mint> answer(K+1);
for(int i=0; i<patterns.size(); ++i) {
const vector<int>& p = patterns[i];
int k = 0;
for(int x=0; x<4; ++x) k += (pa[x]==p[pb[x]]);
if(k <= K) {
int N = max(*max_element(p.begin(), p.end()) - (AN-1), 0);
answer[k] += counting(N, C-AN);
}
}
return answer;
}
void generate_patterns(bool ALLOW_DUP, int LEN, vector<bool>& used,
vector<int>& p, int next, vector< vector<int> >& patterns)
{
if(p.size() == LEN)
patterns.push_back(p);
else
for(int v=0; v<=next; ++v) if(ALLOW_DUP || !used[v]) {
p.push_back(v);
used[v] = true;
generate_patterns(ALLOW_DUP, LEN, used, p, max(v+1, next), patterns);
used[v] = false;
p.pop_back();
}
}
};
// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
{ ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
{ os << "{ ";
for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
bool ok = (Expected == Received);
if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl;
cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END verify_case(_, TheBrickTowerHardDivOne().find(C, K, H));}
int main(){
CASE(0)
int C = 2;
int K = 0;
long long H = 2LL;
int _ = 4;
END
CASE(2)
int C = 2;
int K = 3;
long long H = 1LL;
int _ = 14;
END
CASE(3)
int C = 4;
int K = 7;
long long H = 47LL;
int _ = 1008981254;
END
CASE(4)
int C = 4747;
int K = 7;
long long H = 474747474747474747LL;
int _ = 473182063;
END
}
// END CUT HERE