For g (x) % MOD; obviously, X must have a cyclic knot;
Proof:
As long as g (x) = g (K), g (x-1) = g (k-1) exists, a loop occurs, while G (x) the value of is changed within the finite field (1-mod), so the arrangement of g (x) and g (x-1) is also limited, while X is infinite, therefore, g (x) = g (K), g (x-1) = g (k-1 );
Then we can find the MOD in each layer function, which is
Mod1 = 1000000007;
Mod2 = 222222224;
Mod3= 183120;
Mod4 = 240;
So the I-layer function in the remainder Modi, the median remainder Mod (I-1 );
G (n) = 3 * g (n-1) + g (n-2 );
G [1] 1g [N] 3 1g [n-1]
= *
G [0] 0g [n-1] 1 0g [n-2]
The matrix power can be obtained, similar to the fast power calculation method, binary Calculation
#include<stdio.h>const __int64 mod1 = 1000000007;const __int64 mod3 = 183120;const __int64 mod2 = 222222224, mod4 = 240;__int64 result[241];int copy(__int64 b[][2], __int64 a[][2]){b[0][0] = a[0][0];b[0][1] = a[0][1];b[1][0] = a[1][0];b[1][1] = a[1][1];return 0;}__int64 f(__int64 a[][2], __int64 x, __int64 b[][2], __int64 mod){__int64 temp[2][2];if(x == 1){copy(b, a);return 0;}f(a, x / 2, temp, mod);b[0][0] = (temp[0][0] * temp[0][0] + temp[0][1] * temp[1][0]) % mod;b[0][1] = (temp[0][0] * temp[0][1] + temp[0][1] * temp[1][1]) % mod;b[1][0] = (temp[1][0] * temp[0][0] + temp[1][1] * temp[1][0]) % mod;b[1][1] = (temp[1][0] * temp[0][1] + temp[1][1] * temp[1][1]) % mod;if(x % 2 == 0)return 0;copy(temp, b);b[0][0] = (temp[0][0] * a[0][0] + temp[0][1] * a[1][0]) % mod;b[0][1] = (temp[0][0] * a[0][1] + temp[0][1] * a[1][1]) % mod;b[1][0] = (temp[1][0] * a[0][0] + temp[1][1] * a[1][0]) % mod;b[1][1] = (temp[1][0] * a[0][1] + temp[1][1] * a[1][1]) % mod;return 0;}__int64 g(__int64 x, __int64 mod){__int64 a[2][2], b[2][2];if(x < 2)return x;a[0][0] = 3;a[0][1] = 1;a[1][0] = 1;a[1][1] = 0;f(a, x - 1, b, mod);return b[0][0];}int init(){int i;result[0] = 0;result[1] = 1;for(i = 2; i <= 240; i ++){result[i] = g(g(g(i % mod4, mod3) % mod3, mod2) % mod2, mod1) % mod1;}return 0;}int main(){__int64 n;init();while(scanf("%I64d", &n) != EOF){printf("%I64d\n", result[n % mod4]);}return 0;}