#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e5 + 5, mod = 1e9;
struct matrix {
LL a[12][12];
matrix operator * (matrix x) {
matrix c;
memset(c.a, 0, sizeof c.a);
for (int i = 0; i <= 8; i ++)
for (int k = 0; k <= 8; k ++)
for (int j = 0; j <= 8; j ++)
c.a[i][j] = (c.a[i][j] + a[i][k] * x.a[k][j]) % mod;
return c;
}
} bas, f, f1[N], f2[N];
LL n, phi, cnt;
matrix query(LL k) { return f1[k % cnt] * f2[k / cnt]; }
LL getPhi(LL k) {
LL res = 1;
for (int i = 2; i * i <= k; i ++) {
if (k % i) continue;
res *= i - 1, k /= i;
while (k % i == 0) k /= i, res *= i;
}
if (k != 1) res *= k - 1;
return res;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
for (int i = 0; i <= 8; i ++) {
bas.a[i][i] = 4;
if (i > 0) bas.a[i][i - 1] = 9 - i;
if (i < 8) bas.a[i][i + 1] = i + 1;
}
phi = getPhi(mod), cnt = sqrt(phi * 2) + 1, f.a[4][0] = 1;
for (int i = 0; i <= 8; i ++) f1[0].a[i][i] = f2[0].a[i][i] = 1;
for (int i = 1; i <= cnt; i ++) f1[i] = f1[i - 1] * bas;
for (int i = 1; i <= cnt; i ++) f2[i] = f2[i - 1] * f1[cnt];
while (cin >> n, n) {
if (n >= phi) n = n % phi + phi;
cout << (query(n) * f).a[8][0] << '\n';
}
return 0;
}