Your code works fine with smaller values of nn (I tried with nn=100). Perhaps your full problem is too big with your approach.