program colorful_hats_2
  implicit none
  integer(8), parameter :: md = 1000000007_8
  integer :: n, a(100000) = 0, i, c(3) = 0, j
  integer(8) :: ans = 1_8, m
  read(*,*) n
  read(*,*) a(1:n)
  do i = 1, n
    m = 0_8
    do j = 1, 3
      if (a(i) == c(j)) m = m+1_8
    end do
    ans = mod(ans*m,md)
    do j = 1, 3
      if (a(i) == c(j)) then
        c(j) = c(j)+1
        exit
      end if
    end do
  end do
  write(*,'(i0)') ans
end program colorful_hats_2
; ModuleID = 'FIRModule'
source_filename = "FIRModule"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"

@_QFEa = internal global [100000 x i32] zeroinitializer
@_QFEans = internal unnamed_addr global i64 1
@_QFEc = internal unnamed_addr global [3 x i32] zeroinitializer
@_QQcl.2a6c080d06a9b31992d85021d03cc8fb = linkonce constant [73 x i8] c"/home/ubuntu/Downloads/TheStack_IR/Collated/Fortran/Source_2905/source.f\00"
@_QQcl.28693029 = linkonce constant [4 x i8] c"(i0)"
@_QQEnvironmentDefaults = local_unnamed_addr constant ptr null

define void @_QQmain() local_unnamed_addr {
  %1 = alloca { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, align 8
  %2 = alloca i32, align 4
  %3 = tail call ptr @_FortranAioBeginExternalListInput(i32 -1, ptr nonnull @_QQcl.2a6c080d06a9b31992d85021d03cc8fb, i32 6)
  %4 = call i1 @_FortranAioInputInteger(ptr %3, ptr nonnull %2, i32 4)
  %5 = call i32 @_FortranAioEndIoStatement(ptr %3)
  %6 = call ptr @_FortranAioBeginExternalListInput(i32 -1, ptr nonnull @_QQcl.2a6c080d06a9b31992d85021d03cc8fb, i32 7)
  %7 = load i32, ptr %2, align 4, !tbaa !3
  %8 = call i32 @llvm.smax.i32(i32 %7, i32 0)
  %9 = zext i32 %8 to i64
  store ptr @_QFEa, ptr %1, align 8, !tbaa !7
  %.fca.1.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 1
  store i64 4, ptr %.fca.1.gep, align 8, !tbaa !7
  %.fca.2.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 2
  store i32 20180515, ptr %.fca.2.gep, align 8, !tbaa !7
  %.fca.3.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 3
  store i8 1, ptr %.fca.3.gep, align 4, !tbaa !7
  %.fca.4.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 4
  store i8 9, ptr %.fca.4.gep, align 1, !tbaa !7
  %.fca.5.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 5
  store i8 0, ptr %.fca.5.gep, align 2, !tbaa !7
  %.fca.6.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 6
  store i8 0, ptr %.fca.6.gep, align 1, !tbaa !7
  %.fca.7.0.0.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 7, i64 0, i64 0
  store i64 1, ptr %.fca.7.0.0.gep, align 8, !tbaa !7
  %.fca.7.0.1.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 7, i64 0, i64 1
  store i64 %9, ptr %.fca.7.0.1.gep, align 8, !tbaa !7
  %.fca.7.0.2.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 7, i64 0, i64 2
  store i64 4, ptr %.fca.7.0.2.gep, align 8, !tbaa !7
  %10 = call i1 @_FortranAioInputDescriptor(ptr %6, ptr nonnull %1)
  %11 = call i32 @_FortranAioEndIoStatement(ptr %6)
  %12 = load i32, ptr %2, align 4, !tbaa !3
  %13 = icmp sgt i32 %12, 0
  br i1 %13, label %.preheader.preheader, label %36

.preheader.preheader:                             ; preds = %0
  %_QFEans.promoted = load i64, ptr @_QFEans, align 8, !tbaa !3
  %.pre32 = load i32, ptr @_QFEc, align 4, !tbaa !3
  %.pre34 = load i32, ptr getelementptr inbounds ([3 x i32], ptr @_QFEc, i64 0, i64 1), align 4, !tbaa !3
  %.pre36 = load i32, ptr getelementptr inbounds ([3 x i32], ptr @_QFEc, i64 0, i64 2), align 4, !tbaa !3
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %.loopexit
  %14 = phi i32 [ %32, %.loopexit ], [ %.pre36, %.preheader.preheader ]
  %15 = phi i32 [ %33, %.loopexit ], [ %.pre34, %.preheader.preheader ]
  %16 = phi i32 [ %34, %.loopexit ], [ %.pre32, %.preheader.preheader ]
  %.01525 = phi i32 [ %35, %.loopexit ], [ 1, %.preheader.preheader ]
  %17 = phi i64 [ %29, %.loopexit ], [ %_QFEans.promoted, %.preheader.preheader ]
  %18 = sext i32 %.01525 to i64
  %19 = add nsw i64 %18, -1
  %20 = getelementptr [100000 x i32], ptr @_QFEa, i64 0, i64 %19
  %21 = load i32, ptr %20, align 4, !tbaa !3
  %22 = icmp eq i32 %21, %16
  %23 = zext i1 %22 to i64
  %24 = icmp eq i32 %21, %15
  %25 = zext i1 %24 to i64
  %spec.select.1 = add nuw nsw i64 %23, %25
  %26 = icmp eq i32 %21, %14
  %27 = zext i1 %26 to i64
  %spec.select.2 = add nuw nsw i64 %spec.select.1, %27
  %28 = mul i64 %17, %spec.select.2
  %29 = srem i64 %28, 1000000007
  %brmerge = select i1 %22, i1 true, i1 %24
  %brmerge37 = select i1 %brmerge, i1 true, i1 %26
  br i1 %brmerge37, label %30, label %.loopexit

30:                                               ; preds = %.preheader
  %_QFEc.mux = select i1 %24, ptr getelementptr inbounds ([3 x i32], ptr @_QFEc, i64 0, i64 1), ptr getelementptr inbounds ([3 x i32], ptr @_QFEc, i64 0, i64 2)
  %_QFEc.mux.mux = select i1 %22, ptr @_QFEc, ptr %_QFEc.mux
  %31 = add i32 %21, 1
  store i32 %31, ptr %_QFEc.mux.mux, align 4, !tbaa !3
  %.pre = load i32, ptr @_QFEc, align 4, !tbaa !3
  %.pre33 = load i32, ptr getelementptr inbounds ([3 x i32], ptr @_QFEc, i64 0, i64 1), align 4, !tbaa !3
  %.pre35 = load i32, ptr getelementptr inbounds ([3 x i32], ptr @_QFEc, i64 0, i64 2), align 4, !tbaa !3
  br label %.loopexit

.loopexit:                                        ; preds = %.preheader, %30
  %32 = phi i32 [ %.pre35, %30 ], [ %14, %.preheader ]
  %33 = phi i32 [ %.pre33, %30 ], [ %15, %.preheader ]
  %34 = phi i32 [ %.pre, %30 ], [ %16, %.preheader ]
  %35 = add nuw i32 %.01525, 1
  %exitcond.not = icmp eq i32 %.01525, %12
  br i1 %exitcond.not, label %._crit_edge, label %.preheader

._crit_edge:                                      ; preds = %.loopexit
  store i64 %29, ptr @_QFEans, align 8, !tbaa !3
  br label %36

36:                                               ; preds = %._crit_edge, %0
  %37 = call ptr @_FortranAioBeginExternalFormattedOutput(ptr nonnull @_QQcl.28693029, i64 4, ptr null, i32 -1, ptr nonnull @_QQcl.2a6c080d06a9b31992d85021d03cc8fb, i32 21)
  %38 = load i64, ptr @_QFEans, align 8, !tbaa !3
  %39 = call i1 @_FortranAioOutputInteger64(ptr %37, i64 %38)
  %40 = call i32 @_FortranAioEndIoStatement(ptr %37)
  ret void
}

declare ptr @_FortranAioBeginExternalListInput(i32, ptr, i32) local_unnamed_addr

declare i1 @_FortranAioInputInteger(ptr, ptr, i32) local_unnamed_addr

declare i32 @_FortranAioEndIoStatement(ptr) local_unnamed_addr

declare i1 @_FortranAioInputDescriptor(ptr, ptr) local_unnamed_addr

declare ptr @_FortranAioBeginExternalFormattedOutput(ptr, i64, ptr, i32, ptr, i32) local_unnamed_addr

declare i1 @_FortranAioOutputInteger64(ptr, i64) local_unnamed_addr

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #0

attributes #0 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!llvm.module.flags = !{!0, !1, !2}

!0 = !{i32 2, !"Debug Info Version", i32 3}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{!4, !4, i64 0}
!4 = !{!"any data access", !5, i64 0}
!5 = !{!"any access", !6, i64 0}
!6 = !{!"Flang Type TBAA Root"}
!7 = !{!8, !8, i64 0}
!8 = !{!"descriptor member", !5, i64 0}
