Then Ilúvatar spoke, and he said: ‘Mighty are the Ainur, and mightiest among them is Melkor; but that he may know, and all the Ainur, that I am Ilúvatar, those things that ye have sung, I will show them forth, that ye may see what ye have done. And thou, Melkor, shalt see that no theme may be played that hath not its uttermost source in me, nor can any alter the music in my despite. For he that attempteth this shall prove but mine instrument in the devising of things more wonderful, which he himself hath not imagined.’
— Tolkien, The Silmarillion, 6.
“You planned evil against me; God planned it for good to bring about the present result—the survival of many people” (Genesis 50:20 CSB).
We know that all things work together for the good of those who love God, who are called according to his purpose (Romans 8:28 CSB).