TLE for SUMTRIAN - python 2

Getting TLE for the code. How to optimize it further??
code:py2