Chef and Segments-Tle

please view my solution, although i have used repeated squaring, still i am getting tle.
http://www.codechef.com/viewsolution/7229062

Thanks in advance