We know that Moriarty had to get someone who looked like Sherlock to kidnap those kids in Reichenbach. We also know that Tom resembled Sherlock and it’s a theory that he was one of the assassins. Maybe he was the one Moriarty used.
When Sherlock came back, Moriarty knew because well, he’s a spider at the centre of a web, but he now knew that Molly helped Sherlock and was where Moriarty messed up. Could it be possible that Moriarty set up Tom and Molly to somehow spy on Sherlock?