The line "... repeated 996 more times" together with the 4 error lines shown gives 1000 Lines which is (afaik) the limit of the error stack.
Advice from 50 years of develoment: even stackspace is endless.
If you convert your formula to use Arrays (Python: Lists) and Arraypointers. you may notice the memory the formula needs.
Hint: Lists can allocate a lot of memory for your case.