Here iterative implementation of pow

#include <iostream>#include <vector>// Multiply a NxN (square) matrix % MODstd::vector<std::vector<long>> operator*(const std::vector<std::vector<long>>& lhs,const std::vector<std::vector<long>>& rhs){int N = lhs.size();std::vector<std::vector<long>> res (N, std::vector<long>(N, 0L));// Multiply -> O(n^3)for (int i = 0; i < N; ++i)for (int j = 0; j < N; ++j)for (int k = 0; k < N; ++k)res[i][j] = (res[i][j] + (lhs[i][k] * rhs[k][j])) % 1000000007;return res;}// Binary exponentiation of a square matrixstd::vector<std::vector<long>> power(std::vector<std::vector<long>> base, int exp) // pass by value to work on a copy{