so it actually starts long after FtR
what’s happening is at the end of FtR, he gets transported inside the confession dial, into a hell made specifically for him. the time lords created it to try and get out of him what the hybrid is, how it’s created, how to use it etc. the Veil is a nightmare creature based on something he saw as a child, a woman who had died and they’d put veils over but it was hot and the flies had come anyway–so probably the reason she’s so monstrous is because, in spite of having been just a normal woman, she looms large in the memory of the child than she would in the memory of an adult.
so anyway, what he figures out is that if he keeps confessing, he keeps living. the Veil won’t touch him when he’s telling truths. whenever he’s close to death, or trying to escape, he mentally escapes into his safe space, which is his TARDIS where he can talk to Clara and hash things out. eventually, once he’s mapped the prison out, he finds that the exit is in room 12, through a wall made of some material multiple times stronger than diamond.
the time lords probably planned it so that he would have to confess everything about the hybrid to have a shot at escaping. what they didn’t plan on was that he would figure out a different route: pick away at the wall, be killed (knowing that it takes a long time for a time lord to actually succumb to fatal wounds), and re-create himself in the transportation chamber. he keeps making copies of himself using the imprint left in the transport unit + the energy of his own dying body. every time, right before he dies, he scrawls “bird” into the sand–a reminder of a Brothers Grimm story called The Shepherd Boy, where a king asks the boy “how long is eternity,” and he tells a story of a bird who sharpens its beak on a mountain made of diamond–when the mountain is worn away, that’s the end of the first second of eternity.
by the time the episode starts, he’s been doing this for 7,000 years. the wall barely has a dent in it, but because he’s essentially the same person each time, he keeps figuring it out at roughly the same time. but he also keeps being horrified at how long he’s been at it, keeps mourning Clara over and over. it seems like while he’s figuring it out, he’s also remembering or imagining that he remembers–so by the time he finally breaks through, it’s been 2 billion years and he knows he has been trapped there, reliving all that pain over and over, both physically and emotionally.
at the end, when he breaks through the wall, it’s revealed that the prison had been inside a confession dial (whether his own or whether it was a different one, swapped out in secret or just created separately, I don’t know). the Veil had been a clockwork monster. the prison had existed inside another universe, and he was back on Gallifrey, where he’s set to confront the people who trapped him inside the prison in the first place. the time lords mistakenly thought he knew how to find or create a dalek-time lord hybrid, which was prophesied to rain down terror upon Gallifrey, but he reveals that he’s the hybrid that will destroy Gallifrey. he was scared of the prophesy as a boy but now that it was the Time Lords’ plan that killed Clara, it sounds like he’s letting the grief and rage turn him into the creature of prophecy, the one that will destroy a billion hearts to heal his own.
FYI – Perfect Summary