#include #include constexpr int mod = 1000'000'007; using matrix_2D = std::array; matrix_2D mult(const matrix_2D& x, const matrix_2D& y){ return matrix_2D({ (x[0] * y[0] + x[1] * y[2]) % mod, (x[0] * y[1] + x[1] * y[3]) % mod, (x[2] * y[0] + x[3] * y[2]) % mod, (x[2] * y[1] + x[3] * y[3]) % mod }); } matrix_2D power(const matrix_2D& a, int n){ if(n == 0) return matrix_2D({1, 0, 0, 1}); const matrix_2D p = power(a, n / 2); const matrix_2D res = mult(p, p); if(n & 1) return mult(a, res); return res; } int main(void){ std::cin.tie(nullptr); std::cin.sync_with_stdio(false); std::cout.tie(nullptr); int P, K; std::cin >> P >> K; const matrix_2D a = matrix_2D({2 * (P - 1), 1LL, 2 * (P - 1), P + 1}); const matrix_2D x = power(a, K); std::cout << x[3] << '\n'; return 0; }