快速模幂

typedef long long LL;
const LL MOD = 100000007;
LL qpow(LL x, LL n)
{
    LL result = 1;
    while(n) {
        if(n & 1)
            result = result*x%MOD;
        n >>= 1;
        x = x*x%MOD;
    }
    return result;
}

矩阵快速模幂

typedef long long ll;
const ll MOD = 1000000007;
struct _Matrix{
    int size;
    ll matrix[10][10];
    _Matrix(int _size){
        size = _size;
    }
    _Matrix(){
        size = 10;
    }
    void init0(){
        memset(matrix, 0, sizeof(matrix));
    }
    void init1(){
        memset(matrix, 0, sizeof(matrix));
        for(int i = 0; i < size; ++i) matrix[i][i] = 1;
    }
    _Matrix Mutiple(_Matrix rhs){
        _Matrix result(4);
        for(int i = 0; i < size; ++i){
            for(int j = 0; j < size; ++j){
                result.matrix[i][j] = 0;
                for(int k = 0; k < size; ++k){
                    result.matrix[i][j] += (rhs.matrix[i][k] * matrix[k][j] + MOD)%MOD;
                    result.matrix[i][j] %= MOD;
                }
            }
        }
        return result;
    }
};

_Matrix qpow(_Matrix a, ll b){
    _Matrix m(4);
    m.init1();
    while(b){
        if(b&1) m = a.Mutiple(m);
        a=a.Mutiple(a);
        b >>= 1;
    }
    return m;
}