user
Find the last   **40**   decimal digits of   

a

b
{\displaystyle {\displaystyle a^{b}}}
,   where

:   :   * a
          =
          2988348162058574136915891421498819466320163312926952423791023078876139
          {\displaystyle {\displaystyle a=2988348162058574136915891421498819466320163312926952423791023078876139}}
        * b
          =
          2351399303373464486466122544523690094744975233415544072992656881240319
          {\displaystyle {\displaystyle b=2351399303373464486466122544523690094744975233415544072992656881240319}}

A computer is too slow to find the entire value of   

a

b
{\displaystyle {\displaystyle a^{b}}}
.

Instead, the program must use a fast algorithm for modular exponentiation:   

a

b

mod


m
{\displaystyle {\displaystyle a^{b}\mod m}}
.

The algorithm must work for any integers   

a
,
b
,
m
{\displaystyle {\displaystyle a,b,m}}
,     where   

b
≥
0
{\displaystyle {\displaystyle b\geq 0}}
   and   

m
>
0
{\displaystyle {\displaystyle m>0}}
.
assistant
(formerly Perl 6)
This is specced as a built-in, but here's an explicit version:

```
sub expmod(Int $a is copy, Int $b is copy, $n) {
    my $c = 1;
    repeat while $b div= 2 {
        ($c *= $a) %= $n if $b % 2;
        ($a *= $a) %= $n;
    }
    $c;
}

say expmod
    2988348162058574136915891421498819466320163312926952423791023078876139,
    2351399303373464486466122544523690094744975233415544072992656881240319,
    10**40;

```

Output:

```
1527229998585248450016808958343740453059
```