It's pretty hard to make insanity a crime. They have no clue they have gone insane, but insane people shouldn't be around children.
It’s not hard at all. Should not matter if the guilty are insane. In fact, contrary to this whole insanity defense thing, if someone is (in this case) a pervert because they are crazy that should be more reason to restrain them, not an excuse to less.