returns x^n in O(logn) time and O(1) space