[POLYCODE][CodeFusion] Time limit exceeded

This solution got time limit exceeded. I checked the other codes and they all are similar to mine. May I know where I might be exceeding the time limit?

Thanks in advance

The problem was with the val array. I thought the precomputation would speed up the process but instead it was slowing down the code and also I had to use 10^6 elements instead of 10^5 in val array