def polynomial_reduce_mod(poly, polymod, p):
 assert (polymod[(-1)] == 1)
 assert (len(polymod) > 1)
 while (len(poly) >= len(polymod)):
  if (poly[(-1)] != 0):
   for i in range(2, (len(polymod) + 1)):
    poly[(- i)] = ((poly[(- i)] - (poly[(-1)] * polymod[(- i)])) % p)
  poly = poly[0:(-1)]
 return poly

def polynomial_reduce_mod ( poly , polymod , p ) : assert ( polymod [ ( - 1 ) ] == 1 ) assert ( len ( polymod ) > 1 ) while ( len ( poly ) >= len ( polymod ) ) : if ( poly [ ( - 1 ) ] != 0 ) : for i in range ( 2 , ( len ( polymod ) + 1 ) ) : poly [ ( - i ) ] = ( ( poly [ ( - i ) ] - ( poly [ ( - 1 ) ] * polymod [ ( - i ) ] ) ) % p ) poly = poly [ 0 : ( - 1 ) ] return poly
reduce poly by polymod .