I'm implementing Shor's algorithm from scratch and therefore want to implement a unitary gate $U$ such that $U^{2^j}|y\rangle = |a^{2^j}y \: \text{mod} \: N\rangle$. I know that an efficient way of doing this is by following the procedure presented by Stephane Beauregard in Circuit for Shor's algorithm using 2n+3 qubits; and this is what I'm going to be using.
Out of curiosity, I wanted to know if there is any way of building these unitaries without resorting to some clever insights like done in the paper or other efficient implementations, i.e. if there is something more beginner friendly. I was specifically wondering if there is a way of constructing the matrix for $U^{2^j}$ and then just use Qiskit's unitary to gate function. I know this would be very inefficient, but does anyone know how to do this?
I found this answer that does what I'm asking but it relies on already knowing the order $r$. Is there some way to do it for any $a$ and $N$ without having to know the order of $a^x \: \text{mod} \: N$ beforehand?