IDENTIFICATION DIVISION.
PROGRAM-ID. 159C.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
  SELECT STDIO ASSIGN TO KEYBOARD. 
*>
DATA DIVISION.
FILE SECTION.
FD STDIO.
01 INP        PIC X(1400000).
*>
WORKING-STORAGE SECTION.
01 N          PIC  9(6).
01 IN-TBL.
   03 A       PIC  9(6) OCCURS 200000.
01 CNT-TBL.
   03 C       PIC  9(6) OCCURS 200000.
01 P1         PIC  9(6).
01 P2         PIC  9(6).
01 I          PIC  9(6).
01 J          PIC  9(6).
01 X          PIC S9(6).
01 CNT        PIC  9(11).
01 OUT        PIC  Z(10)9.
*> 
PROCEDURE DIVISION.
MAIN-001.
  ACCEPT N.
  OPEN INPUT STDIO.
  READ STDIO.
  CLOSE STDIO.
*>
  PERFORM VARYING I FROM 1 BY 1 UNTIL I > N
    MOVE 0   TO C(I)
  END-PERFORM.
*>
  MOVE 1 TO P1.
  PERFORM VARYING I FROM 1 BY 1 UNTIL I > N
    PERFORM VARYING P2 FROM P1 BY 1 UNTIL INP(P2:1) = ' '
    END-PERFORM
    MOVE INP(P1:P2 - P1) TO A(I)
    ADD  1               TO C(A(I))
*>
    COMPUTE P1 = P2 + 1
  END-PERFORM.
*>  
  PERFORM VARYING I FROM 1 BY 1 UNTIL I > N
    MOVE 0 TO CNT
    PERFORM VARYING J FROM 1 BY 1 UNTIL J > N
      IF (C(J) > 1)
        IF (A(I) = J)
          COMPUTE X = C(J) - 1
        ELSE
          COMPUTE X = C(J)
        END-IF
        IF (X > 1)
          COMPUTE CNT = CNT + X * (X - 1) / 2
        END-IF
      END-IF 
    END-PERFORM
*>
    MOVE CNT TO OUT
    DISPLAY FUNCTION TRIM(OUT)
  END-PERFORM.
*>
  STOP RUN.
