Initially I wanted to use modulo operations, but accidentally fell into a big pit. Need to pay attention to the correct calculation method for negative number modulo in the future.
intmod(intx,intm){return(x%m+m)%m;}
Insight: When performing modulo operations on negative numbers in different languages, the results may be different.