; LLVM IR from tangjz/acm-icpc :: 51nod (1122)

; Unoptimized IR (clang -disable-llvm-passes):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

%struct.Matrix = type { i32, i32, [4 x [4 x i32]] }

@n = external dso_local global i32, align 4
@seq = external dso_local global [4 x i32], align 16
@ans = external dso_local global i32, align 4
@A = external dso_local global %struct.Matrix, align 4
@constinit = external hidden constant [4 x [4 x i32]], align 4
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca %struct.Matrix, align 4
  %3 = alloca i32, align 4
  %4 = alloca %struct.Matrix, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 72, ptr %2) #6
  %7 = getelementptr inbounds %struct.Matrix, ptr %2, i32 0, i32 0
  store i32 4, ptr %7, align 4, !tbaa !5
  %8 = getelementptr inbounds %struct.Matrix, ptr %2, i32 0, i32 1
  store i32 4, ptr %8, align 4, !tbaa !10
  %9 = getelementptr inbounds %struct.Matrix, ptr %2, i32 0, i32 2
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %9, ptr align 4 @constinit, i64 64, i1 false), !tbaa.struct !11
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 @A, ptr align 4 %2, i64 72, i1 false), !tbaa.struct !13
  call void @llvm.lifetime.end.p0(i64 72, ptr %2) #6
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #6
  store i32 0, ptr %3, align 4, !tbaa !14
  br label %10

10:                                               ; preds = %19, %0
  %11 = load i32, ptr %3, align 4, !tbaa !14
  %12 = icmp slt i32 %11, 4
  br i1 %12, label %14, label %13

13:                                               ; preds = %10
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #6
  br label %22

14:                                               ; preds = %10
  %15 = load i32, ptr %3, align 4, !tbaa !14
  %16 = load i32, ptr %3, align 4, !tbaa !14
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [4 x i32], ptr @seq, i64 0, i64 %17
  store i32 %15, ptr %18, align 4, !tbaa !14
  br label %19

19:                                               ; preds = %14
  %20 = load i32, ptr %3, align 4, !tbaa !14
  %21 = add nsw i32 %20, 1
  store i32 %21, ptr %3, align 4, !tbaa !14
  br label %10, !llvm.loop !15

22:                                               ; preds = %13
  %23 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @n)
  call void @llvm.lifetime.start.p0(i64 72, ptr %4) #6
  %24 = load i32, ptr @n, align 4, !tbaa !14
  call void @_ZNK6Matrix3powEi(ptr dead_on_unwind writable sret(%struct.Matrix) align 4 %4, ptr noundef nonnull align 4 dereferenceable(72) @A, i32 noundef %24)
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 @A, ptr align 4 %4, i64 72, i1 false), !tbaa.struct !13
  call void @llvm.lifetime.end.p0(i64 72, ptr %4) #6
  br label %25

25:                                               ; preds = %60, %22
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #6
  store i32 1, ptr %5, align 4, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #6
  store i32 0, ptr %6, align 4, !tbaa !14
  br label %26

26:                                               ; preds = %47, %25
  %27 = load i32, ptr %6, align 4, !tbaa !14
  %28 = icmp slt i32 %27, 4
  br i1 %28, label %30, label %29

29:                                               ; preds = %26
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #6
  br label %50

30:                                               ; preds = %26
  %31 = load i32, ptr %5, align 4, !tbaa !14
  %32 = sext i32 %31 to i64
  %33 = load i32, ptr %6, align 4, !tbaa !14
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [4 x [4 x i32]], ptr getelementptr inbounds (%struct.Matrix, ptr @A, i32 0, i32 2), i64 0, i64 %34
  %36 = load i32, ptr %6, align 4, !tbaa !14
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [4 x i32], ptr @seq, i64 0, i64 %37
  %39 = load i32, ptr %38, align 4, !tbaa !14
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [4 x i32], ptr %35, i64 0, i64 %40
  %42 = load i32, ptr %41, align 4, !tbaa !14
  %43 = sext i32 %42 to i64
  %44 = mul nsw i64 %32, %43
  %45 = srem i64 %44, 1000000007
  %46 = trunc i64 %45 to i32
  store i32 %46, ptr %5, align 4, !tbaa !14
  br label %47

47:                                               ; preds = %30
  %48 = load i32, ptr %6, align 4, !tbaa !14
  %49 = add nsw i32 %48, 1
  store i32 %49, ptr %6, align 4, !tbaa !14
  br label %26, !llvm.loop !17

50:                                               ; preds = %29
  %51 = load i32, ptr %5, align 4, !tbaa !14
  %52 = load i32, ptr @ans, align 4, !tbaa !14
  %53 = add nsw i32 %52, %51
  store i32 %53, ptr @ans, align 4, !tbaa !14
  %54 = load i32, ptr @ans, align 4, !tbaa !14
  %55 = icmp sge i32 %54, 1000000007
  br i1 %55, label %56, label %59

56:                                               ; preds = %50
  %57 = load i32, ptr @ans, align 4, !tbaa !14
  %58 = sub nsw i32 %57, 1000000007
  store i32 %58, ptr @ans, align 4, !tbaa !14
  br label %59

59:                                               ; preds = %56, %50
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #6
  br label %60

60:                                               ; preds = %59
  %61 = call noundef zeroext i1 @_ZSt16next_permutationIPiEbT_S1_(ptr noundef @seq, ptr noundef getelementptr inbounds (i32, ptr @seq, i64 4))
  br i1 %61, label %25, label %62, !llvm.loop !18

62:                                               ; preds = %60
  %63 = load i32, ptr @ans, align 4, !tbaa !14
  %64 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %63)
  ret i32 0
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

declare i32 @__isoc99_scanf(ptr noundef, ...) #3

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZNK6Matrix3powEi(ptr dead_on_unwind noalias writable sret(%struct.Matrix) align 4, ptr noundef nonnull align 4 dereferenceable(72), i32 noundef) #4 align 2

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef zeroext i1 @_ZSt16next_permutationIPiEbT_S1_(ptr noundef, ptr noundef) #5

declare i32 @printf(ptr noundef, ...) #3

attributes #0 = { mustprogress norecurse uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #4 = { mustprogress uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #5 = { inlinehint mustprogress uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #6 = { nounwind }

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

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !7, i64 0}
!6 = !{!"_ZTS6Matrix", !7, i64 0, !7, i64 4, !8, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!6, !7, i64 4}
!11 = !{i64 0, i64 64, !12}
!12 = !{!8, !8, i64 0}
!13 = !{i64 0, i64 4, !14, i64 4, i64 4, !14, i64 8, i64 64, !12}
!14 = !{!7, !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !16}
!18 = distinct !{!18, !16}


; Optimized IR (opt -passes='default<O3>'):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

%struct.Matrix = type { i32, i32, [4 x [4 x i32]] }

@n = external dso_local global i32, align 4
@seq = external dso_local global [4 x i32], align 16
@ans = external dso_local local_unnamed_addr global i32, align 4
@A = external dso_local global %struct.Matrix, align 4
@constinit = external hidden local_unnamed_addr constant [4 x [4 x i32]], align 4
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = alloca %struct.Matrix, align 4
  store i32 4, ptr @A, align 4, !tbaa !5
  store i32 4, ptr getelementptr inbounds (i8, ptr @A, i64 4), align 4, !tbaa !5
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(64) getelementptr inbounds (i8, ptr @A, i64 8), ptr noundef nonnull align 4 dereferenceable(64) @constinit, i64 64, i1 false), !tbaa.struct !9
  store <4 x i32> <i32 0, i32 1, i32 2, i32 3>, ptr @seq, align 16, !tbaa !5
  %2 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @n)
  call void @llvm.lifetime.start.p0(i64 72, ptr nonnull %1) #6
  %3 = load i32, ptr @n, align 4, !tbaa !5
  call void @_ZNK6Matrix3powEi(ptr dead_on_unwind nonnull writable sret(%struct.Matrix) align 4 %1, ptr noundef nonnull align 4 dereferenceable(72) @A, i32 noundef %3)
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(72) @A, ptr noundef nonnull align 4 dereferenceable(72) %1, i64 72, i1 false), !tbaa.struct !11
  call void @llvm.lifetime.end.p0(i64 72, ptr nonnull %1) #6
  br label %4

4:                                                ; preds = %4, %0
  %5 = load i32, ptr @seq, align 16, !tbaa !5
  %6 = sext i32 %5 to i64
  %7 = getelementptr inbounds [4 x [4 x i32]], ptr getelementptr inbounds (i8, ptr @A, i64 8), i64 0, i64 0, i64 %6
  %8 = load i32, ptr %7, align 4, !tbaa !5
  %9 = srem i32 %8, 1000000007
  %.sext = sext i32 %9 to i64
  %10 = load i32, ptr getelementptr inbounds (i8, ptr @seq, i64 4), align 4, !tbaa !5
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds [4 x [4 x i32]], ptr getelementptr inbounds (i8, ptr @A, i64 8), i64 0, i64 1, i64 %11
  %13 = load i32, ptr %12, align 4, !tbaa !5
  %14 = sext i32 %13 to i64
  %15 = mul nsw i64 %.sext, %14
  %16 = srem i64 %15, 1000000007
  %17 = load i32, ptr getelementptr inbounds (i8, ptr @seq, i64 8), align 8, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [4 x [4 x i32]], ptr getelementptr inbounds (i8, ptr @A, i64 8), i64 0, i64 2, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = sext i32 %20 to i64
  %22 = mul nsw i64 %16, %21
  %23 = srem i64 %22, 1000000007
  %24 = load i32, ptr getelementptr inbounds (i8, ptr @seq, i64 12), align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [4 x [4 x i32]], ptr getelementptr inbounds (i8, ptr @A, i64 8), i64 0, i64 3, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = mul nsw i64 %23, %28
  %30 = srem i64 %29, 1000000007
  %31 = trunc nsw i64 %30 to i32
  %32 = load i32, ptr @ans, align 4, !tbaa !5
  %33 = add nsw i32 %32, %31
  %34 = icmp sgt i32 %33, 1000000006
  %35 = add nsw i32 %33, -1000000007
  %spec.select = select i1 %34, i32 %35, i32 %33
  store i32 %spec.select, ptr @ans, align 4, !tbaa !5
  %36 = call noundef zeroext i1 @_ZSt16next_permutationIPiEbT_S1_(ptr noundef nonnull @seq, ptr noundef nonnull getelementptr inbounds (i8, ptr @seq, i64 16))
  br i1 %36, label %4, label %37, !llvm.loop !12

37:                                               ; preds = %4
  %38 = load i32, ptr @ans, align 4, !tbaa !5
  %39 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %38)
  ret i32 0
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nofree nounwind
declare noundef i32 @__isoc99_scanf(ptr nocapture noundef readonly, ...) local_unnamed_addr #3

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZNK6Matrix3powEi(ptr dead_on_unwind noalias writable sret(%struct.Matrix) align 4, ptr noundef nonnull align 4 dereferenceable(72), i32 noundef) local_unnamed_addr #4 align 2

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef zeroext i1 @_ZSt16next_permutationIPiEbT_S1_(ptr noundef, ptr noundef) local_unnamed_addr #5

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #3

attributes #0 = { mustprogress norecurse uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { nofree nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #4 = { mustprogress uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #5 = { inlinehint mustprogress uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #6 = { nounwind }

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

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{i64 0, i64 64, !10}
!10 = !{!7, !7, i64 0}
!11 = !{i64 0, i64 4, !5, i64 4, i64 4, !5, i64 8, i64 64, !10}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,LCSSAPass,LoopRotatePass,ReassociatePass,SLPVectorizerPass,SROAPass,SimplifyCFGPass,TailCallElimPass