Your code might be failing for larger test-cases?


#1

Do I need to improve on the time complexity cause already it is O(N) ?


#2

No, O(n) is fine. You probably missed to return the result modulo 10^9 + 7.