Just learnt Binary Matrix Exponentiation for Codeforces 166E problem from: I will be glad if someone help me out explaining his solution, 
I found this in comment section and finally got it,
andTETRAHEDRON : can this problem be think in a way that if u want to reach at D in n steps, then to travel n — 1 steps 3 ^ (n — 1) ways possible, now this case include that if u reached at D in n — 1 steps therefore to reach in n steps ans(in n steps) = 3 ^ (n — 1) — ans(in n — 1 steps); which is a DP solution eg — to reach in 1 steps = 0 to reach in 2 steps — 3 ^ (2 — 1) — 0; to reach in 3 steps — 3 ^ (3 — 1) — 3 = 6 to reach in 4 steps — 3 ^ (4 — 1) — 6 = 21 to reach in 5 steps — 3 ^ (5 — 1) — 21 = 60 andLet f[A][j] is the number of ways from D to A by going j steps,as same,f[B][j], f[C][j] and f[D][j], we know f[D][0] = 1, f[A, B, C][0] = 0(because the ant starts at D). then you get four equations below: f[A][j] = f[B][j  1] + f[C][j  1] + f[D][j  1] f[B][j] = f[A][j  1] + f[C][j  1] + f[D][j  1] ...... f[D][j] = ... you can solve this problem in O(n),the answer is f[D][n] mod 1e9+7. but it's too slow.you can express the four equations by matrix.then it's obvious to solve the problem by matrix fast power algorithm in O(lg n). answered 12 Nov '17, 00:12
