reserve a,b,m,x,n,l,xi,xj for Nat,
  t,z for Integer;

theorem Th18:
  a <> 0 & m > 1 & a,m are_coprime implies (a |^ Euler m) mod m = 1
proof
  assume that
A1: a <> 0 and
A2: m > 1 and
A3: a,m are_coprime;
  set X = {k where k is Element of NAT : m,k are_coprime & k >= 1 & k <= m};
A4: a |^ Euler m <> 0 by A1,PREPOWER:5;
  set Y = {l where l is Element of NAT : ex u being Element of NAT st l = a*u
  mod m & u in X};
A5: x in X implies a*x mod m in X
  proof
    assume x in X;
    then consider t being Element of NAT such that
A6: t = x and
A7: m,t are_coprime and
A8: t >= 1 and
    t <= m;
A9: a*t,m are_coprime by A1,A2,A3,A7,EULER_1:14;
    a*t mod m <> 0
    proof
      assume a*t mod m = 0;
      then consider s being Nat such that
A10:  a*t = m*s+0 and
      0 < m by A2,NAT_D:def 2;
      s gcd 1 = 1 by NEWTON:51;
      then m*s gcd m*1 = m by EULER_1:5;
      hence contradiction by A2,A9,A10;
    end;
    then
A11: a*t mod m >= 1 by NAT_1:14;
A12: a*t mod m <= m by A2,NAT_D:1;
    m,a*t mod m are_coprime by A1,A2,A3,A7,A8,Th3;
    hence thesis by A6,A11,A12;
  end;
A13: X = Y
  proof
    thus X c= Y
    proof
      let x be object;
      assume x in X;
      then consider xx being Element of NAT such that
A14:  x = xx and
A15:  m,xx are_coprime and
      xx >= 1 and
A16:  xx <= m;
      consider i,j being Integer such that
A17:  i*a+j*m = xx by A3,EULER_1:10;
      xx <> m by A2,A15,EULER_1:1;
      then xx < m by A16,XXREAL_0:1;
      then
A18:  xx mod m = xx by NAT_D:24;
      reconsider im = i mod m as Element of NAT by INT_1:3,NEWTON:64;
      i mod m <> 0
      proof
        assume i mod m = 0;
        then 0 = i-(i div m)*m by A2,INT_1:def 10;
        then m gcd xx = m*1 gcd m*((i div m)*a+j) by A17
          .= m*(1 gcd ((i div m)*a+j)) by EULER_1:15
          .= m*1 by Lm3
          .= m;
        hence contradiction by A2,A15;
      end;
      then
A19:  im >= 1 by NAT_1:14;
A20:  i = (i div m)*m+(i mod m) by A2,NEWTON:66;
      then reconsider
      ij = (i mod m)*a+(((i div m)*a)+j)*m as Element of NAT by A17;
A21:  im <= m by A2,NEWTON:65;
A22:  ij mod m = im*a mod m by NAT_D:61;
      then m,im are_coprime by A2,A15,A18,A17,A20,Th4;
      then im in X by A19,A21;
      hence thesis by A14,A18,A17,A20,A22;
    end;
    let y be object;
    assume y in Y;
    then
    ex yy being Element of NAT st y = yy & ex u being Element of NAT st yy
    = a*u mod m & u in X;
    hence thesis by A5;
  end;
A23: xi in X & xj in X & xi <> xj implies a*xi mod m <> a*xj mod m
  proof
    assume that
A24: xi in X and
A25: xj in X and
A26: xi <> xj;
    set tm = a*xi mod m,sm = a*xj mod m;
    assume
A27: a*xi mod m = a*xj mod m;
    consider s being Element of NAT such that
A28: s = xj and
    m,s are_coprime and
A29: s >= 1 & s <= m by A25;
A30: sm = (a*s)-m*((a*s) div m) by A2,A28,NEWTON:63;
    consider t being Element of NAT such that
A31: t = xi and
    m,t are_coprime and
A32: t >= 1 & t <= m by A24;
    tm = (a*t)-m*((a*t) div m) by A2,A31,NEWTON:63;
    then 0 = a*(t-s)-m*(((a*t) div m)-((a*s) div m)) by A27,A30;
    then
A33: m divides a*(t-s) by INT_1:def 3;
    reconsider m,c = (t-s) as Integer;
    1-m <= c & c <= m-1 by A32,A29,XREAL_1:13;
    then t-s = 0 by A3,A33,Lm4,INT_2:25;
    hence contradiction by A26,A31,A28;
  end;
  reconsider FX = Sgm X as FinSequence of NAT;
  reconsider FYY = (a*FX) as natural-valued FinSequence;
  reconsider FY = FYY mod m as natural-valued FinSequence;
A35: X c= Seg m
  proof
    let x be object;
    assume x in X;
    then
    ex xx being Element of NAT st x = xx & m,xx are_coprime & xx >=
    1 & xx <= m;
    hence thesis by FINSEQ_1:1;
  end;
  then reconsider X as finite set;
a35: X is included_in_Seg by A35,FINSEQ_1:def 13;
  dom FYY = Seg(len FYY) by FINSEQ_1:def 3
    .= Seg(len FX) by NEWTON:2;
  then
A36: dom FX = dom FYY by FINSEQ_1:def 3;
A37: dom FY = Seg(len FY) by FINSEQ_1:def 3
    .= Seg(len FYY) by Def2
    .= Seg(len FX) by NEWTON:2;
  then
A38: dom FX = dom FY by FINSEQ_1:def 3;
A39: rng FY = Y
  proof
    thus rng FY c= Y
    proof
      let x be object;
      assume x in rng FY;
      then consider y being object such that
A40:  y in dom FY and
A41:  x = FY.y by FUNCT_1:def 3;
      reconsider y as Element of NAT by A40;
      FX.y in rng FX by A38,A40,FUNCT_1:def 3;
      then
A42:  FX.y in X by a35,FINSEQ_1:def 14;
      y in dom FX by A37,A40,FINSEQ_1:def 3;
      then FY.y = (FYY.y) mod m by A36,Def1
        .= (a*FX.y) mod m by RVSUM_1:44;
      hence thesis by A41,A42;
    end;
    let y be object;
    assume y in Y;
    then consider yy being Element of NAT such that
A43: y = yy and
A44: ex u being Element of NAT st yy = a*u mod m & u in X;
    consider uu being Element of NAT such that
A45: yy = a*uu mod m and
A46: uu in X by A44;
    uu in rng FX by a35,A46,FINSEQ_1:def 14;
    then consider xx being object such that
A47: xx in dom FX and
A48: uu = FX.xx by FUNCT_1:def 3;
    reconsider xx as Element of NAT by A47;
    FY.xx = (FYY.xx) mod m by A36,A47,Def1
      .= y by A43,A45,A48,RVSUM_1:44;
    hence thesis by A38,A47,FUNCT_1:def 3;
  end;
A49: FY is one-to-one
  proof
A50: FX is one-to-one by a35,FINSEQ_3:92;
    let x1,x2 be object;
    assume that
A51: x1 in dom FY and
A52: x2 in dom FY and
A53: FY.x1 = FY.x2;
A54: x2 in dom FX by A37,A52,FINSEQ_1:def 3;
    reconsider x2 as Element of NAT by A52;
A55: x1 in dom FX by A37,A51,FINSEQ_1:def 3;
    reconsider x1 as Element of NAT by A51;
A56: FX.x1 in rng FX & FX.x2 in rng FX by A38,A51,A52,FUNCT_1:def 3;
A57: FY.x2 = (FYY.x2) mod m by A36,A54,Def1
      .= (a*FX.x2) mod m by RVSUM_1:44;
    FY.x1 = (FYY.x1) mod m by A36,A55,Def1
      .= (a*FX.x1) mod m by RVSUM_1:44;
    then not(FX.x1 in X & FX.x2 in X & FX.x1 <> FX.x2) by A23,A53,A57;
    hence thesis by a35,A55,A54,A56,A50,FINSEQ_1:def 14;
  end;
  for x be object holds card Coim(FX,x) = card Coim(FY,x)
  proof
    let x be object;
      per cases;
      suppose
A58:    x in X;
A59:    FX is one-to-one by a35,FINSEQ_3:92;
        x in rng FX by a35,A58,FINSEQ_1:def 14;
        then
A60:    len(FX-{x}) = len(FX)-1 by A59,FINSEQ_3:90;
A61:    len(FX-{x})-len(FX-{x}) = len(FX)-card(FX"{x})-len(FX-{x}) & len
        (FY-{x}) = len(FY)-card(FY"{x}) by FINSEQ_3:59;
        len(FY-{x}) = len(FY)-1 by A13,A39,A49,A58,FINSEQ_3:90;
        hence thesis by A60,A61;
      end;
      suppose
        not x in X;
        then ( not x in rng FX)& FY"{x} = {} by A13,a35,A39,FINSEQ_1:def 14
,FUNCT_1:72;
        hence thesis by FUNCT_1:72;
      end;
  end;
  then
A62: FX,FY are_fiberwise_equipotent by CLASSES1:def 10;
  then
A63: len(FX) = len(FY) by RFINSEQ:3;
A64: (Product FY) mod m = (a |^ len(FY)*Product FX) mod m
  proof
    defpred P[Nat] means $1 <= len(FY) implies (Product (FY|$1)) mod m = (a |^
    len(FY|$1)*Product (FX|$1)) mod m;
    FY|len FY = FY|(Seg len FY) by FINSEQ_1:def 16;
    then
A65: FY = FY|len FY by FINSEQ_2:20;
A66: for n st P[n] holds P[n+1]
    proof
      let n;
      now
        per cases;
        suppose
A67:      n+1 <= len FY;
          then len (FX|(n+1)) = n+1 by A63,FINSEQ_1:59;
          then
A68:      FX|(n+1) = (FX|(n+1))|n ^ <* (FX|(n+1)).(n+1) *> by RFINSEQ:7;
          assume
A69:      P[n];
          0+1 <= n+1 by NAT_1:13;
          then
A70:      n+1 in dom FY by A67,FINSEQ_3:25;
A71:      (FY|(n+1)).(n+1) = FY.(n+1) by FINSEQ_3:112;
A72:      (FX|(n+1)).(n+1) = FX.(n+1) by FINSEQ_3:112;
A73:      n <= n+1 by NAT_1:11;
          then
A74:      len(FY|n) = n by A67,FINSEQ_1:59,XXREAL_0:2;
A75:      (FX|(n+1))|n = FX|n by A73,FINSEQ_5:77;
A76:      (FY|(n+1))|n = FY|n by A73,FINSEQ_5:77;
A77:      len (FY|(n+1)) = n+1 by A67,FINSEQ_1:59;
          then FY|(n+1) = (FY|(n+1))|n ^ <* (FY|(n+1)).(n+1) *> by RFINSEQ:7;
          then (Product(FY|(n+1))) mod m = (Product(FY|n)*FY.(n+1)) mod m by
A76,A71,RVSUM_1:96
            .= (((a |^ len(FY|n)*Product(FX|n)) mod m) *(FY.(n+1) mod m))
          mod m by A67,A69,A73,NAT_D:67,XXREAL_0:2
            .= (((a |^ len(FY|n)*Product(FX|n)) mod m) *((FYY.(n+1) mod m)
          mod m)) mod m by A38,A36,A70,Def1
            .= (((a |^ len(FY|n)*Product(FX|n)) mod m) *((a*FX).(n+1) mod m)
          ) mod m by NAT_D:73
            .= ((a |^ len(FY|n)*Product(FX|n))*(a*FX).(n+1)) mod m by NAT_D:67
            .= (a |^ n*Product (FX|n))*(a*(FX.(n+1))) mod m by A74,RVSUM_1:44
            .= (a |^ n*a)*(Product(FX|n)*(FX.(n+1))) mod m
            .= (a |^ len(FY|(n+1)))*(Product(FX|n)*(FX.(n+1))) mod m by A77,
NEWTON:6
            .= (a |^ len(FY|(n+1)))*(Product(FX|(n+1))) mod m by A68,A75,A72,
RVSUM_1:96;
          hence P[n+1];
        end;
        suppose
          n+1 > len FY;
          hence thesis;
        end;
      end;
      hence thesis;
    end;
    FX|len FX = FX|(Seg len FX) by FINSEQ_1:def 16;
    then
A78: FX = FX|len FY by A63,FINSEQ_2:20;
A79: P[0] by NEWTON:4,RVSUM_1:94;
    for n holds P[n] from NAT_1:sch 2(A79,A66);
    hence thesis by A65,A78;
  end;
A80: (Product FX),m are_coprime
  proof
    defpred P[Nat] means $1 <= len FX implies (Product (FX|$1)),m
    are_coprime;
A81: FX|len(FX) = FX by FINSEQ_1:58;
A82: for n st P[n] holds P[n+1]
    proof
      let n;
      now
        per cases;
        suppose
A83:      len FX >= n+1;
          reconsider FF = (FX|(n+1)) as FinSequence of NAT;
          reconsider ff = FF.(n+1) as Element of NAT;
          len FF = n+1 by A83,FINSEQ_1:59;
          then
A84:      FF = (FF|n) ^ <*ff*> by RFINSEQ:7;
          reconsider a = (Product (FF|n)),b = ff,m9 = m as Integer;
A85:      n <= n+1 by NAT_1:11;
          0+1 <= (n+1) by XREAL_1:6;
          then (FX|(n+1)).(n+1) = FX.(n+1) & n+1 in dom FX by A83,FINSEQ_3:25
,112;
          then
A86:      (FX|(n+1)).(n+1) in rng FX by FUNCT_1:def 3;
          rng FX = X by a35,FINSEQ_1:def 14;
          then
A87:      ex p being Element of NAT st ff = p & m,p are_coprime &
          p >= 1 & p <= m by A86;
          assume P[n];
          then a,m9 are_coprime by A83,A85,FINSEQ_5:77,XXREAL_0:2;
          then a*b,m9 are_coprime by A87,INT_2:26;
          hence P[n+1] by A84,RVSUM_1:96;
        end;
        suppose
          len FX < n+1;
          hence thesis;
        end;
      end;
      hence thesis;
    end;
    (Product (FX|0)) gcd m = 1 by NEWTON:51,RVSUM_1:94;
    then
A88: P[0] by INT_2:def 3;
    for n holds P[n] from NAT_1:sch 2(A88,A82);
    hence thesis by A81;
  end;
  len(FX) = card X by a35,FINSEQ_3:39
    .= Euler m by EULER_1:def 1;
  then
A89: len FY = Euler m by A62,RFINSEQ:3;
  (Product FX) mod m = (Product FY) mod m by A62,Th10;
  hence thesis by A2,A4,A64,A89,A80,Th12;
end;
