 : 0^ ( x -- z )
     [ 0/0. ] [ 0 < 1/0. 0 ? ] if-zero ; inline
 
-: (^mod) ( n x y -- z )
-    make-bits 1 [
-        [ dupd * pick mod ] when [ sq over mod ] dip
-    ] reduce 2nip ; inline
+: (^mod) ( x y n -- z )
+    [ make-bits 1 ] dip dup
+    '[ [ over * _ mod ] when [ sq _ mod ] dip ] reduce nip ; inline
 
 : (gcd) ( b a x y -- a d )
     over zero? [

! Copyright (C) 2004, 2010 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: math kernel math.constants math.private math.bits
math.libm combinators math.order sequences ;
IN: math.functions

: >fraction ( a/b -- a b )

    [ non-trivial-divisor ] if ; foldable

: ^mod ( x y n -- z )
    over 0 <
    [ [ [ neg ] dip ^mod ] keep mod-inv ] [ (^mod) ] if ; foldable

GENERIC: absq ( x -- y ) foldable

