I know , how to calculate " x mod p " for a given very large "x" value and some p value. Here , x and p are integers

But how can we calculate f(x) mod g(x) , for which f(x) has higher degree(or order) than g(x).

And that degree turns out to be a very large number.

Ex:[ (x+1)^1729 mod ((x^5)-1) ] or [ (x+1)^1729 mod (1729,((x^5)-1)) ]

Is there any method or algorithm to calculate it at faster speeds