It looks like something may be tripping up a mod_security rule on the server, that can happen from time to time if the rules are too strict.
How to check:
Make the error happen using the field, make a note of the time
Check server logs to see if any mod_sec rules have been triggered
Disable any triggered rules
Test the field again, is mod_sec was the ...
Right, so essentially, you've got two channel entries loops going here, and they're not at all related. You'll either need to reconfigure your code to use only one, or try to pass a row_id parameter from the first set of images into the modal.
You could do this several ways - via embed, or stash potentially, etc, etc. I'm not familiar with the gallery you'...