reserve i,i1,i2,i3,i4,i5,j,r,a,b,x,y for Integer,
  d,e,k,n for Nat,
  fp,fk for FinSequence of INT,
  f,f1,f2 for FinSequence of REAL,
  p for Prime;
reserve fr for FinSequence of REAL;
reserve fr,f for FinSequence of INT;
reserve b,m for Nat;
reserve b for Integer;
reserve m for Integer;
reserve fp for FinSequence of NAT;
reserve a,m for Nat;

theorem Th41:
  for f be FinSequence of NAT st p>2 & a gcd p = 1 & f = a*(idseq
((p-'1) div 2)) & m=card{k where k is Element of NAT:k in rng (f mod p) & k > p
  /2} holds Lege(a,p) = (-1)|^m
proof
  let f be FinSequence of NAT;
  assume that
A1: p>2 and
A2: a gcd p = 1 and
A3: f = a*(idseq ((p-'1) div 2)) and
A4: m=card{k where k is Element of NAT:k in rng (f mod p) & k > p/2};
  set f1 = f mod p;
A5: len f1 = len f by EULER_2:def 2;
  set X = {k where k is Element of NAT:k in rng f1 & k > p/2};
  for x being object st x in X holds x in rng f1
  proof
    let x be object;
    assume x in X;
    then ex k being Element of NAT st x = k & k in rng f1 & k > p/2;
    hence thesis;
  end;
  then
A6: X c= rng f1;
  then reconsider X as finite set;
  reconsider X as finite Subset of NAT by A6,XBOOLE_1:1;
  card X is Element of NAT;
  then reconsider m as Element of NAT by A4;
A7: rng f1 \ X c= rng f1 by XBOOLE_1:36;
  reconsider Y = rng f1 \ X as finite Subset of NAT;
A8: a|^((p-'1) div 2),Lege(a,p) are_congruent_mod p by A1,A2,Th28,INT_1:14;
  set f2 = Sgm rng f1;
  (Product f1) mod p = (Product f) mod p by EULER_2:11;
  then
A9: Product f1,Product f are_congruent_mod p by NAT_D:64;
A10: p>1 by INT_2:def 4;
  then
A11: p-'1 = p-1 by XREAL_1:233;
  then
A12: p-'1>0 by A10,XREAL_1:50;
  set p9 = (p-'1) div 2;
 rng idseq p9 = Seg p9;
  then reconsider I = idseq p9 as FinSequence of NAT by FINSEQ_1:def 4;
  dom f = dom I by A3,VALUED_1:def 5;
  then
A13: len f = len I by FINSEQ_3:29
    .= p9 by CARD_1:def 7;
  p >= 2+1 by A1,NAT_1:13;
  then p-1 >= 3-1 by XREAL_1:9;
  then f1 <> {} by A13,A11,A5,NAT_2:13;
  then rng f1 is non empty Subset of NAT;
  then consider n1 being Element of NAT such that
A14: rng f1 c= Seg n1 \/ {0} by HEYTING3:1;
  I is Element of p9-tuples_on REAL by FINSEQ_2:109,NUMBERS:19;
  then
A15: Product f = (Product (p9|->a))*(Product I) by A3,RVSUM_1:108
    .= (a|^p9) * (Product I) by NEWTON:def 1;
  p is odd by A1,PEPIN:17;
  then
A16: p-'1 is even by A11,HILBERT3:2;
  then
A17: p9 = ((p-'1)+1) div 2 by NAT_2:26
    .= p div 2 by A10,XREAL_1:235;
  2 divides (p-'1) by A16,PEPIN:22;
  then
A18: p-'1 = 2*p9;
  then p9 divides (p-'1);
  then p9 <= (p-'1) by A12,NAT_D:7;
  then
A19: p9 < p by A11,XREAL_1:146,XXREAL_0:2;
  for d being Nat st d in dom I holds I.d gcd p = 1
  proof
    let d be Nat;
    assume d in dom I;
    then
A20: d in Seg len I by FINSEQ_1:def 3;
    then
A21: d in Seg p9 by CARD_1:def 7;
    then
A22: I.d = d by FINSEQ_2:49;
    d <= p9 by A21,FINSEQ_1:1;
    then
A23: d < p by A19,XXREAL_0:2;
    d >= 1 by A20,FINSEQ_1:1;
    then d,p are_coprime by A23,EULER_1:2;
    hence thesis by A22,INT_2:def 3;
  end;
  then
A24: (Product I) gcd p = 1 by WSIERP_1:36;
A25: for d st d in dom f holds f.d = a*d
  proof
    let d;
    assume
A26: d in dom f;
    then d in dom I by A3,VALUED_1:def 5;
    then d in Seg len I by FINSEQ_1:def 3;
    then
A27: d is Element of Seg p9 by CARD_1:def 7;
    thus f.d = a * I.d by A3,A26,VALUED_1:def 5
      .= a*d by A27;
  end;
A28: for d,e being Nat st 1<=d & d<e & e<=len f1 holds f1.d <> f1 .e
  proof
    let d,e be Nat;
    assume that
A29: 1<=d and
A30: d<e and
A31: e<=len f1;
A32: e<=len f by A31,EULER_2:def 2;
    1<=e by A29,A30,XXREAL_0:2;
    then
A33: e in dom f by A32,FINSEQ_3:25;
    then
A34: f1.e = (f.e) mod p by EULER_2:def 2;
    d<len f by A30,A32,XXREAL_0:2;
    then
A35: d in dom f by A29,FINSEQ_3:25;
    then
A36: f1.d = (f.d) mod p by EULER_2:def 2;
    now
      assume f1.d = f1.e;
      then f.e,f.d are_congruent_mod p by A36,A34,NAT_D:64;
      then p divides (a*e - f.d) by A25,A33;
      then p divides (a*e - a*d) by A25,A35;
      then
A37:  p divides a*(e-d);
A38:  p9-1<p by A19,XREAL_1:147;
      reconsider dd = e-d as Element of NAT by A30,NAT_1:21;
A39:  |.p.| = p by ABSVALUE:def 1;
A40:  |.dd.| = dd by ABSVALUE:def 1;
A41:  dd <= p9 - 1 by A13,A5,A29,A31,XREAL_1:13;
      dd <> 0 by A30;
      then p <= dd by A2,A37,A39,A40,INT_4:6,WSIERP_1:29;
      hence contradiction by A41,A38,XXREAL_0:2;
    end;
    hence thesis;
  end;
  then
A42: len f1 = card rng f1 by GRAPH_5:7;
  then
A43: f1 is one-to-one by FINSEQ_4:62;
A44: dom f1 = dom f by A5,FINSEQ_3:29;
  not 0 in rng f1
  proof
    reconsider a as Element of NAT by ORDINAL1:def 12;
    assume 0 in rng f1;
    then consider n be Nat such that
A45: n in dom f1 and
A46: f1.n = 0 by FINSEQ_2:10;
    0 = (f.n) mod p by A44,A45,A46,EULER_2:def 2
      .= (a*n) mod p by A25,A44,A45;
    then
A47: p divides (a*n) by PEPIN:6;
    n >= 1 by A45,FINSEQ_3:25;
    then
A48: p <= n by A2,A47,NAT_D:7,WSIERP_1:30;
    n <= p9 by A13,A5,A45,FINSEQ_3:25;
    hence contradiction by A19,A48,XXREAL_0:2;
  end;
  then
A49: {0} misses rng f1 by ZFMISC_1:50;
  then rng f1 c= Seg n1 by A14,XBOOLE_1:73;
  then
ppp: rng f1 is included_in_Seg by FINSEQ_1:def 13;
  then
A50: f2 is one-to-one by FINSEQ_3:92;
A51: rng f1 c= Seg n1 by A14,A49,XBOOLE_1:73;
  then X c= Seg n1 by A6;
     then
a52: X is included_in_Seg by FINSEQ_1:def 13;
  len f = card rng f1 by A5,A28,GRAPH_5:7;
  then reconsider n = p9 - m as Element of NAT by A4,A13,A6,NAT_1:21,43;
  Y c= Seg n1 by A51,A7;
     then
a53: Y is included_in_Seg by FINSEQ_1:def 13;
     rng f1 is included_in_Seg by A51,FINSEQ_1:def 13;
     then
A54: rng f1 = rng f2 by FINSEQ_1:def 14;
  then
A55: Product f1 = Product f2 by A43,A50,EULER_2:10,RFINSEQ:26;
    set f3 = (len (f2/^n) |-> p) - (f2/^n);
    set f4 = (f2|n) ^ f3;
A56: f2/^n is FinSequence of INT by FINSEQ_2:24,NUMBERS:17;
A57: dom f3 = dom(len (f2/^n) |-> p)/\dom(f2/^n) by VALUED_1:12
      .= (Seg len (len(f2/^n) |-> p))/\dom(f2/^n) by FINSEQ_1:def 3
      .= dom(f2/^n) /\ dom(f2/^n) by FINSEQ_1:def 3,CARD_1:def 7
      .= dom(f2/^n);
    then
A58: len f3 = len(f2/^n) by FINSEQ_3:29;
    for k,l being Nat st k in Y & l in X holds k < l
    proof
      let k,l be Nat;
      assume that
A59:  k in Y and
A60:  l in X;
A61:  not k in X by A59,XBOOLE_0:def 5;
A62:  ex l1 being Element of NAT st l1 = l & l1 in rng f1 & l1>p/2 by A60;
      k in rng f1 by A59,XBOOLE_0:def 5;
      then k <= p/2 by A61;
      hence thesis by A62,XXREAL_0:2;
    end;
    then Sgm (Y\/X) = (Sgm Y)^(Sgm X) by a52,a53,FINSEQ_3:42;
    then Sgm (rng f1 \/ X) = (Sgm Y)^(Sgm X) by XBOOLE_1:39;
    then
A63: f2 = (Sgm Y)^(Sgm X) by A6,XBOOLE_1:12;
A64: for d st d in dom f3 holds f3.d = p - (f2/^n).d
    proof
      let d;
      assume
A65:  d in dom f3;
      then d in Seg len(f2/^n) by A57,FINSEQ_1:def 3;
      then (len (f2/^n) |-> p).d = p by FINSEQ_2:57;
      hence thesis by A65,VALUED_1:13;
    end;
A66: len Sgm Y = card Y by a53,FINSEQ_3:39
      .= p9 - m by A4,A13,A5,A6,A42,CARD_2:44;
    then
A67: f2/^n = Sgm X by A63,FINSEQ_5:37;
A68: for d st d in dom f3 holds f3.d > 0 & f3.d <= p9
    proof
      let d;
      reconsider w = f3.d as Element of INT by INT_1:def 2;
      assume
A69:  d in dom f3;
      then (Sgm X).d in rng Sgm X by A67,A57,FUNCT_1:3;
      then (Sgm X).d in X by a52,FINSEQ_1:def 14;
      then
A70:  ex ll be Element of NAT st ll = (Sgm X).d & ll in rng f1 & ll > p/2;
      then consider e being Nat such that
A71:  e in dom f1 and
A72:  f1.e = (f2/^n).d by A67,FINSEQ_2:10;
      (f2/^n).d = f.e mod p by A44,A71,A72,EULER_2:def 2;
      then
A73:  (f2/^n).d < p by NAT_D:1;
A74:  f3.d = p - (f2/^n).d by A64,A69;
      then w < p - p/2 by A67,A70,XREAL_1:10;
      hence thesis by A17,A74,A73,INT_1:54,XREAL_1:50;
    end;
    for d being Nat st d in dom f3 holds f3.d in NAT
    proof
      let d be Nat;
      assume
A76:   d in dom f3;
      f3.d > 0 by A68,A76;
      hence thesis by INT_1:3;
    end;
    then reconsider f3 as FinSequence of NAT by FINSEQ_2:12;
    |.(-1)|^m.| = 1 by SERIES_2:1;
    then
A77: (-1)|^m = 1 or -((-1)|^m) = 1 by ABSVALUE:1;
    f3 is FinSequence of NAT;
    then reconsider f4 as FinSequence of NAT by FINSEQ_1:75;
A78: f2|n = Sgm Y by A63,A66,FINSEQ_3:113,FINSEQ_6:10;
A79: for d st d in dom f4 holds f4.d>0 & f4.d <= p9
    proof
      let d;
      assume
A80:  d in dom f4;
      per cases by A80,FINSEQ_1:25;
      suppose
A81:    d in dom(f2|n);
        reconsider d as Element of NAT by ORDINAL1:def 12;
        (f2|n).d in rng Sgm Y by A78,A81,FUNCT_1:3;
        then
A82:    (f2|n).d in Y by a53,FINSEQ_1:def 14;
        then
A83:    (f2|n).d in rng f1 by XBOOLE_0:def 5;
        not (f2|n).d in X by A82,XBOOLE_0:def 5;
        then (f2|n).d <= p/2 by A83;
        then
A84:    (f2|n).d <= p9 by A17,INT_1:54;
        not (f2|n).d in {0} by A49,A83,XBOOLE_0:3;
        then (f2|n).d <> 0 by TARSKI:def 1;
        hence thesis by A81,A84,FINSEQ_1:def 7;
      end;
      suppose
        ex l being Nat st l in dom f3 & d=len(f2|n)+ l;
        then consider l be Element of NAT such that
A85:    l in dom f3 and
A86:    d = len(f2|n)+ l;
        f4.d = f3.l by A85,A86,FINSEQ_1:def 7;
        hence thesis by A68,A85;
      end;
    end;
A87: f2 = (f2|n)^(f2/^n) by RFINSEQ:8;
    then
A88: f2/^n is one-to-one by A50,FINSEQ_3:91;
    for d,e being Nat st 1<=d & d<e & e<=len f3 holds f3.d <> f3.e
    proof
      let d,e be Nat;
      assume that
A89:  1<=d and
A90:  d<e and
A91:  e<=len f3;
      1<=e by A89,A90,XXREAL_0:2;
      then
A92:  e in dom f3 by A91,FINSEQ_3:25;
      then
A93:  f3.e = p - (f2/^n).e by A64;
      d<len f3 by A90,A91,XXREAL_0:2;
      then
A94: d in dom f3 by A89,FINSEQ_3:25;
      then f3.d = p - (f2/^n).d by A64;
      hence thesis by A88,A57,A90,A94,A92,A93;
    end;
    then len f3 = card rng f3 by GRAPH_5:7;
    then
A95: f3 is one-to-one by FINSEQ_4:62;
A96: len f2 = p9 by ppp,A13,A5,A42,FINSEQ_3:39;
    then
A97: n <= len f2 by XREAL_1:43;
A98: rng(f2|n) misses rng f3
    proof
      assume rng(f2|n) meets rng f3;
      then consider x be object such that
A99: x in rng(f2|n) and
A100: x in rng f3 by XBOOLE_0:3;
      consider e being Nat such that
A101: e in dom f3 and
A102: f3.e = x by A100,FINSEQ_2:10;
      x = p - (f2/^n).e by A64,A101,A102;
      then
A103: x = p - f2.(e+n) by A97,A57,A101,RFINSEQ:def 1;
      e+n in dom f2 by A57,A101,FINSEQ_5:26;
      then consider e1 be Nat such that
A104: e1 in dom f1 and
A105: f1.e1 = f2.(e+n) by A54,FINSEQ_2:10,FUNCT_1:3;
A106: e1 in dom f by A5,A104,FINSEQ_3:29;
A107: e1 <= p9 by A13,A5,A104,FINSEQ_3:25;
      rng(f2|n) c= rng f2 by FINSEQ_5:19;
      then consider d1 be Nat such that
A108: d1 in dom f1 and
A109: f1.d1 = x by A54,A99,FINSEQ_2:10;
      d1 <= p9 by A13,A5,A108,FINSEQ_3:25;
      then d1+e1 <= p9+p9 by A107,XREAL_1:7;
      then
A110: d1+e1 < p by A11,A18,XREAL_1:146,XXREAL_0:2;
      x = f.d1 mod p by A44,A108,A109,EULER_2:def 2;
      then (f.d1 mod p) + f2.(e+n) = p by A103;
      then (f.d1 mod p) + (f.e1 mod p) = p by A105,A106,EULER_2:def 2;
      then ((f.d1 mod p)+(f.e1 mod p)) mod p = 0 by NAT_D:25;
      then (f.d1 + f.e1) mod p = 0 by NAT_D:66;
      then p divides (f.d1 + f.e1) by PEPIN:6;
      then p divides (d1*a + f.e1) by A25,A44,A108;
      then p divides (d1*a + e1*a) by A25,A106;
      then
A111: p divides (d1+e1)*a;
      d1 >= 1 by A108,FINSEQ_3:25;
      hence contradiction by A2,A111,A110,NAT_D:7,WSIERP_1:30;
    end;
    f2|n is one-to-one by A50,A87,FINSEQ_3:91;
    then
A112: f4 is one-to-one by A95,A98,FINSEQ_3:91;
A113: for d st d in dom f3 holds f3.d,-(f2/^n).d are_congruent_mod p
    proof
      let d;
      assume d in dom f3;
      then f3.d mod p = (p - (f2/^n).d) mod p by A64
        .= (1*p + (-(f2/^n).d)) mod p
        .= (-(f2/^n).d) mod p by NAT_D:61;
      hence thesis by NAT_D:64;
    end;
A114: len(f2/^n) = (len f2 -' n) by RFINSEQ:29
      .= len f2 - n by A96,XREAL_1:43,233
      .= m by A96;
    len(f2|n) = n by A96,FINSEQ_1:59,XREAL_1:43;
    then len f4 = n + m by A58,A114,FINSEQ_1:22
      .= len f by A13;
    then rng f4 = rng I by A13,A112,A79,Th40;
    then Product f4 = Product I by A112,EULER_2:10,RFINSEQ:26;
    then
A115: (Product(f2|n)) * (Product f3) = Product I by RVSUM_1:97;
    f3 is FinSequence of INT by FINSEQ_2:24,NUMBERS:17;
    then (Product f3)*(Product(f2|n)),(-1)|^m * Product(f2/^n)*(Product(f2|n)
    ) are_congruent_mod p by A58,A114,A56,A113,Th33,INT_4:11;
    then (Product f3)*(Product(f2|n)),(-1)|^m * ((Product(f2|n))*Product(f2/^
    n ) ) are_congruent_mod p;
    then Product I,(-1)|^m * Product((f2|n)^(f2/^n)) are_congruent_mod p by
A115,RVSUM_1:97;
    then
A116: Product I,(-1)|^m * Product f1 are_congruent_mod p by A55,RFINSEQ:8;
    (-1)|^m * Product f1,(-1)|^m * Product f are_congruent_mod p by A9,
INT_4:11;
    then Product I,(-1)|^m * (a|^p9) * (Product I) are_congruent_mod p by A15
,A116,INT_1:15;
    then p divides (1-((-1)|^m * (a|^p9)))* Product I;
    then p divides (1-((-1)|^m * (a|^p9))) by A24,WSIERP_1:29;
    then p divides (-1)|^m * (1-((-1)|^m * (a|^p9))) by INT_2:2;
    then
A117: p divides ((-1)|^m - (-1)|^m * (-1)|^m * (a|^p9));
    (-1)|^m * (-1)|^m = (-1)|^(m+m) by NEWTON:8
      .= (-1)|^(2*m)
      .= ((-1)|^2)|^m by NEWTON:9
      .= (1|^2)|^m by WSIERP_1:1
      .= 1;
    then (-1)|^m,a|^p9 are_congruent_mod p by A117;
    then
A118: (-1)|^m,Lege(a,p) are_congruent_mod p by A8,INT_1:15;
    per cases by A77;
    suppose
A119: (-1)|^m = 1;
      then
A120: Lege(a,p) <> -1 by A118,A1,NAT_D:7;
      now
        assume Lege(a,p) = 0;
        then p divides (1-0) by A118,A119;
        then p = 1 by WSIERP_1:15;
        hence contradiction by A1;
      end;
      hence thesis by A119,Th25,A120;
    end;
    suppose
A121: (-1)|^m = -1;
A122:    now
        assume Lege(a,p) = 1;
        then p divides (-2) by A118,A121;
        then p divides 2 by INT_2:10;
        hence contradiction by A1,NAT_D:7;
      end;
      now
        assume Lege(a,p) = 0;
        then p divides (-1-0) by A118,A121;
        then p = 1 by WSIERP_1:15,INT_2:10;
        hence contradiction by A1;
      end;
      hence thesis by A121,Th25,A122;
    end;
end;
