hey can someone please check my code for SSTORY problem http://www.codechef.com/problems/SSTORY

http://www.codechef.com/viewplaintext/3937332 i am getting WA and i want to know the reason. i have used hashing + binary search it runs in O(n*(logn)^2). is it because of collisions? thanks for helping.

EDIT : my earlier code was getting WA because my hashing was wrong. there were overflow problems so i corrected it and got AC with same hashing method on a problem at codeforces. so i guess my hashing s correct. but this solution is getting WA here. http://www.codechef.com/viewplaintext/3958374