What @AnthonyCusack said, basically, but if you want more detail… here’s my two penn’orth.
Spoken Welsh uses the same pronouns with verbs as it does for saying that you own something (Literary Welsh does something more complicated) – so at the end of one of the old courses Aran says “thank you for choosing me” (to teach you) – only the Welsh for “choosing me” is fy newis, just like “my hat” is fy het.
Welsh doesn’t let you just end a sentence with something left hanging in quite the same way that English does – so when you might end a sentence in English with “something I can believe in” the Welsh would have to say “something I can believe in it.” (The ‘it’ is the same as the something.) I’m reading a book at the moment, where it describes someone climbing, and it says “several times she nearly fell, trying to find a little bit of rock that she could grab onto to pull herself up…” – only in Welsh it’s actually “a little bit, that she could grab onto it” (i afael ynddo) because you can’t have “onto” without also having “it”.
And then beth, for “what”, is really short for pa beth, “what thing?”
So if we really want to pick the Welsh apart in English until we sound like Yoda, the way that it works for me is “thinking about what to write” = “thinking about what thing, to write it” or “thinking about what thing, for the writing of it” – meddwl am beth i’w ysgrifennu – the ‘it’ refers back to the beth, which is why Anthony said it’s reflexive.
But the other thing is that I think – and, as always, pleased to be corrected by those who know more, if I’m wrong – that what people would actually say misses out a lot of these niceties, and could well come out more like meddwl am be’ i sgwennu (colloquial pronunciation for ysgrifennu)