Overly eager explicit row locking causes deadlock
Would it suffice to SELECT ... FOR UPDATE instead?
Can we look at the entire transaction?  Speeding it up will decrease the frequency of deadlocks.
Even if you find a way around this deadlock, you must prepare to get deadlocks.  Usually, it suffices to re-run starting with the START TRANSACTION.


Another approach might prevent the "over-eager" problem:

Put the list of ids in a table (possibly a TEMPORARY table).  Then JOIN to it instead of using IN ( big list ).