You only need to simulate the clock for 12 hours, from that you generate the permutation array.
Then keep applying the entire permutation array to all balls until all balls have cycled at least once. You only have to check them at the end of each day.
Then you have the cycle length of each ball and compute the LCM of that for all balls.
For 7000 balls, it will take 6281 days for all balls to cycle at least once.
My AC code takes 0.3 seconds to generate the output for 7000 balls.
Check input and AC output for thousands of problems on uDebug!
To optimize even further, iterate each ball individually through the permutation array and calculate the cycle length.
Mark each location as visited so you don't have to repeat the same permutation loops.
All of the cycle lengths can be computed in O(N) instead of O(N * N).
Check input and AC output for thousands of problems on uDebug!