There's really no good answer for this. You could probably make up a number of excuses, like "he got the Ferengi codes from Starfleet Intellegence," or "the Ferengi are just dumbfucks who are easy to hack." But the most likely explanation is "it happened because the script called for it."
The whole plot for that episode was kind of shaky. The reason behind the training exercise was to "prepare for the Borg," but it's run by by a "tactical genius" who's basically just some asshole who's good at 3D space Othello, and doesn't eve pit them against any known Borg tactics. Plus, they only won by cheating, and therefore learned nothing of use from the exercise (certainly not anything that could be used against the Borg).