power m@(Matrix r c _) p
    | r == c = helper m p $ identity r 
    | otherwise = error "power: matrix not squared."
    where
        helper _ 0 ret = ret
        helper a x ret = if (x .&. 1) == 1
            then helper a' x' (multiply' ret a)
            else helper a' x' ret where
                a' = multiply' a a
                x' = x `shiftR` 1
power m@(Matrix r c _) p
    | r == c = helper m p $ identity r 
    | otherwise = error "power: matrix not squared."
    where
        helper _ 0 ret = ret
        helper a x ret = if (x .&. 1) == 1
            then helper a' x' (multiply' ret a)
            else helper a' x' ret where
                a' = multiply' a a
                x' = x `shiftR` 1
power m@(Matrix r c _) p
    | r == c = helper m p $ identity r 
    | otherwise = error "power: matrix not squared."
    where
        helper _ 0 ret = ret
        helper a x ret = if (x .&. 1) == 1
            then helper a' x' (multiply' ret a)
            else helper a' x' ret where
                a' = multiply' a a
                x' = x `shiftR` 1