Problem 364

Comfortable distance seating sequences mod 100000007, N=10^6.

Answer44855254
Output44855254
StatusPASS
Native helperno
Runtime20 ms
Peak memory19392 KB
Time complexityO(n^2) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)?
Space complexityO(n)?
ApproachFlow solutionNot curated
VerdictUnknown

Flow source

# Project Euler 364
# Comfortable distance seating sequences mod 100000007, N=10^6.

extern {
    function calloc(n: i64, size: i64) -> ptr<void>
    function free(p: ptr<void>) -> void
}

function modpow(base: i64, exp: i64, mod: i64) -> i64 {
    let mut r: i64 = 1
    let mut b: i64 = base % mod
    let mut e: i64 = exp
    while e > 0 {
        if e % 2 == 1 { r = (r * b) % mod }
        b = (b * b) % mod
        e = e / 2
    }
    return r
}

function nCk(n: i64, k: i64, fact: ptr<i64>, invfact: ptr<i64>, mod: i64) -> i64 {
    if k < 0 || k > n { return 0 }
    return ((fact[n] * invfact[k]) % mod * invfact[n - k]) % mod
}

function T(n: i64, fact: ptr<i64>, invfact: ptr<i64>, pow2: ptr<i64>, mod: i64) -> i64 {
    let mut res: i64 = 0
    let mut a: i64 = 1
    while a <= 2 {
        let mut b: i64 = 0
        while b <= 1 {
            let L: i64 = n - a - b
            if L >= 0 {
                let mut e: i64 = 0
                if a == 2 { e = e + 1 }
                if b == 1 { e = e + 1 }
                let start_p: i64 = L % 2
                let max_p: i64 = L / 3
                let mut p: i64 = start_p
                while p <= max_p {
                    let rem: i64 = L - 3 * p
                    let q: i64 = rem / 2
                    let k: i64 = 1 + p + q
                    let mut term: i64 = nCk(k - 1, p, fact, invfact, mod)
                    term = (term * pow2[p]) % mod
                    term = (term * fact[k]) % mod
                    term = (term * fact[p + e]) % mod
                    term = (term * fact[k - 1]) % mod
                    res = (res + term) % mod
                    p = p + 2
                }
            }
            b = b + 1
        }
        a = a + 1
    }
    return res
}

function main() -> i32 {
    let MOD: i64 = 100000007
    let N: i64 = 1000000
    let fact: ptr<i64> = calloc(N + 1, 8)
    let invfact: ptr<i64> = calloc(N + 1, 8)
    let pow2: ptr<i64> = calloc(N / 3 + 4, 8)
    if fact == null || invfact == null || pow2 == null { return 1 }

    fact[0] = 1
    let mut i: i64 = 1
    while i <= N {
        fact[i] = (fact[i - 1] * i) % MOD
        i = i + 1
    }
    invfact[N] = modpow(fact[N], MOD - 2, MOD)
    i = N
    while i > 0 {
        invfact[i - 1] = (invfact[i] * i) % MOD
        i = i - 1
    }
    pow2[0] = 1
    i = 1
    while i <= N / 3 + 3 {
        pow2[i] = (pow2[i - 1] * 2) % MOD
        i = i + 1
    }

    let ans: i64 = T(N, fact, invfact, pow2, MOD)
    printf("%lld\n", ans)
    free(pow2)
    free(invfact)
    free(fact)
    return 0
}

Generated C

#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Flow runtime helpers */
typedef struct flow_temp_node { struct flow_temp_node* next; } flow_temp_node;
static flow_temp_node* flow_temp_head = NULL;
static int flow_temp_atexit_set = 0;
__attribute__((unused)) static void flow_temp_free_all(void) {
    while (flow_temp_head) {
        flow_temp_node* n = flow_temp_head;
        flow_temp_head = n->next;
        free(n);
    }
}
__attribute__((unused)) static void* flow_temp_alloc(size_t nbytes) {
    flow_temp_node* node = (flow_temp_node*)malloc(sizeof(flow_temp_node) + nbytes);
    if (!node) return NULL;
    node->next = flow_temp_head;
    flow_temp_head = node;
    if (!flow_temp_atexit_set) {
        flow_temp_atexit_set = 1;
        atexit(flow_temp_free_all);
    }
    return (void*)(node + 1);
}
#ifndef FLOW_DIAG
#define FLOW_DIAG(msg) fprintf(stderr, "%s", (msg))
#endif
#ifndef FLOW_LOG
#define FLOW_LOG(fmt, ...) printf(fmt, __VA_ARGS__)
#endif
#ifndef FLOW_LOG_EMPTY
#define FLOW_LOG_EMPTY(fmt) printf(fmt)
#endif
static char* flow_strcat(const char* a, const char* b) {
    size_t la = strlen(a ? a : ""), lb = strlen(b ? b : "");
    char* r = (char*)flow_temp_alloc(la + lb + 1);
    if (!r) return NULL;
    if (la) memcpy(r, a, la);
    if (lb) memcpy(r + la, b, lb);
    r[la + lb] = '\0';
    return r;
}

#define __flow_in_arr(arr, val) __extension__ ({ \
    int _found = 0; \
    size_t _n = sizeof(arr)/sizeof((arr)[0]); \
    for (size_t _i = 0; _i < _n; _i++) { \
        if ((arr)[_i] == (val)) { _found = 1; break; } \
    } _found; })

/* Unified fault handler (MISRA #279) — override with -DFLOW_FAULT_HANDLER=fn */
#ifndef FLOW_FAULT_HANDLER
__attribute__((unused)) static inline void flow_fault_handler(const char* msg) {
    fprintf(stderr, "flow: %s\n", msg ? msg : "fault");
    abort();
#if defined(__GNUC__) || defined(__clang__)
    __builtin_unreachable();
#endif
}
#else
#define flow_fault_handler FLOW_FAULT_HANDLER
#endif
#define flow_div_by_zero_handler() flow_fault_handler("division by zero")
#define flow_shift_ub_handler() flow_fault_handler("invalid shift (amount out of range or left-shift of negative)")

#ifndef FLOW_CHECKED_DIV
#define FLOW_CHECKED_DIV(L, R) (((R) != 0) ? ((L) / (R)) : (flow_div_by_zero_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_MOD
#define FLOW_CHECKED_MOD(L, R) (((R) != 0) ? ((L) % (R)) : (flow_div_by_zero_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_SHL
#define FLOW_CHECKED_SHL(L, R) ((((R) >= 0) && ((unsigned long long)(R) < (sizeof(L) * 8ull)) && ((L) >= 0)) ? ((L) << (R)) : (flow_shift_ub_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_SHR
#define FLOW_CHECKED_SHR(L, R) ((((R) >= 0) && ((unsigned long long)(R) < (sizeof(L) * 8ull))) ? ((L) >> (R)) : (flow_shift_ub_handler(), (L) * 0))
#endif

#include <math.h>

void* _ui_state = NULL;

static inline float i32_to_f32(int32_t v) { return (float)v; }

/* Host stub for @gpu kernels (device codegen replaces this). */
static inline int32_t gpu_thread_id(void) { return 0; }

int64_t modpow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod);
int64_t nCk_i64_i64_ptr_i64_ptr_i64_i64(int64_t n, int64_t k, int64_t* fact, int64_t* invfact, int64_t mod);
int64_t T_i64_ptr_i64_ptr_i64_ptr_i64_i64(int64_t n, int64_t* fact, int64_t* invfact, int64_t* pow2, int64_t mod);
int32_t main(void);



int64_t modpow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod) {
    int64_t r = 1;
    int64_t b = FLOW_CHECKED_MOD((base), (mod));
    int64_t e = exp;
    while (e > 0) {
        if (FLOW_CHECKED_MOD((e), (2)) == 1) {
            r = FLOW_CHECKED_MOD(((r * b)), (mod));
        }
        b = FLOW_CHECKED_MOD(((b * b)), (mod));
        e = FLOW_CHECKED_DIV((e), (2));
    }
    return r;
}

int64_t nCk_i64_i64_ptr_i64_ptr_i64_i64(int64_t n, int64_t k, int64_t* fact, int64_t* invfact, int64_t mod) {
    if ((k < 0 || k > n)) {
        return 0;
    }
    return FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((fact[n] * invfact[k])), (mod)) * invfact[(n - k)])), (mod));
}

int64_t T_i64_ptr_i64_ptr_i64_ptr_i64_i64(int64_t n, int64_t* fact, int64_t* invfact, int64_t* pow2, int64_t mod) {
    int64_t res = 0;
    int64_t a = 1;
    while (a <= 2) {
        int64_t b = 0;
        while (b <= 1) {
            int64_t L = ((n - a) - b);
            if (L >= 0) {
                int64_t e = 0;
                if (a == 2) {
                    e = (e + 1);
                }
                if (b == 1) {
                    e = (e + 1);
                }
                int64_t start_p = FLOW_CHECKED_MOD((L), (2));
                int64_t max_p = FLOW_CHECKED_DIV((L), (3));
                int64_t p = start_p;
                while (p <= max_p) {
                    int64_t rem = (L - (3 * p));
                    int64_t q = FLOW_CHECKED_DIV((rem), (2));
                    int64_t k = ((1 + p) + q);
                    int64_t term = nCk_i64_i64_ptr_i64_ptr_i64_i64((k - 1), p, fact, invfact, mod);
                    term = FLOW_CHECKED_MOD(((term * pow2[p])), (mod));
                    term = FLOW_CHECKED_MOD(((term * fact[k])), (mod));
                    term = FLOW_CHECKED_MOD(((term * fact[(p + e)])), (mod));
                    term = FLOW_CHECKED_MOD(((term * fact[(k - 1)])), (mod));
                    res = FLOW_CHECKED_MOD(((res + term)), (mod));
                    p = (p + 2);
                }
            }
            b = (b + 1);
        }
        a = (a + 1);
    }
    return res;
}

int32_t main(void) {
    int64_t MOD = 100000007;
    int64_t N = 1000000;
    int64_t* fact = (int64_t*)(calloc((N + 1), 8));
    int64_t* invfact = (int64_t*)(calloc((N + 1), 8));
    int64_t* pow2 = (int64_t*)(calloc((FLOW_CHECKED_DIV((N), (3)) + 4), 8));
    if (((fact == NULL || invfact == NULL) || pow2 == NULL)) {
        return 1;
    }
    fact[0] = 1;
    int64_t i = 1;
    while (i <= N) {
        fact[i] = FLOW_CHECKED_MOD(((fact[(i - 1)] * i)), (MOD));
        i = (i + 1);
    }
    invfact[N] = modpow_i64_i64_i64(fact[N], (MOD - 2), MOD);
    i = N;
    while (i > 0) {
        invfact[(i - 1)] = FLOW_CHECKED_MOD(((invfact[i] * i)), (MOD));
        i = (i - 1);
    }
    pow2[0] = 1;
    i = 1;
    while (i <= (FLOW_CHECKED_DIV((N), (3)) + 3)) {
        pow2[i] = FLOW_CHECKED_MOD(((pow2[(i - 1)] * 2)), (MOD));
        i = (i + 1);
    }
    int64_t ans = T_i64_ptr_i64_ptr_i64_ptr_i64_i64(N, fact, invfact, pow2, MOD);
    printf("%lld\n", ans);
    free(pow2);
    free(invfact);
    free(fact);
    return 0;
}

Generated MLIR

module {
  llvm.func @printf(!llvm.ptr, ...) -> i32
  llvm.mlir.global internal constant @str_0("%lld\n\00") {addr_space = 0 : i32} : !llvm.array<6 x i8>
  func.func private @calloc(i64, i64) -> !llvm.ptr
  func.func private @free(!llvm.ptr) -> ()
  func.func @modpow(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
    %0 = arith.constant 1 : i32
    %1 = arith.extsi %0 : i32 to i64
    %2 = llvm.mlir.constant(1 : i64) : i64
    %3 = llvm.alloca %2 x i64 : (i64) -> !llvm.ptr
    llvm.store %1, %3 : i64, !llvm.ptr
    %4 = arith.remsi %arg0, %arg2 : i64
    %5 = llvm.mlir.constant(1 : i64) : i64
    %6 = llvm.alloca %5 x i64 : (i64) -> !llvm.ptr
    llvm.store %4, %6 : i64, !llvm.ptr
    %7 = llvm.mlir.constant(1 : i64) : i64
    %8 = llvm.alloca %7 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %8 : i64, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %9 = llvm.load %8 : !llvm.ptr -> i64
    %10 = arith.constant 0 : i32
    %12 = arith.extsi %10 : i32 to i64
    %11 = arith.cmpi sgt, %9, %12 : i64
    cf.cond_br %11, ^bb1, ^bb2
    ^bb1:
      %13 = llvm.load %8 : !llvm.ptr -> i64
      %14 = arith.constant 2 : i32
      %16 = arith.extsi %14 : i32 to i64
      %15 = arith.remsi %13, %16 : i64
      %17 = arith.constant 1 : i32
      %19 = arith.extsi %17 : i32 to i64
      %18 = arith.cmpi eq, %15, %19 : i64
      cf.cond_br %18, ^bb3, ^bb4
      ^bb3:
        %20 = llvm.load %3 : !llvm.ptr -> i64
        %21 = llvm.load %6 : !llvm.ptr -> i64
        %22 = arith.muli %20, %21 : i64
        %23 = arith.remsi %22, %arg2 : i64
        llvm.store %23, %3 : i64, !llvm.ptr
        cf.br ^bb5
      ^bb4:
        cf.br ^bb5
      ^bb5:
      %24 = llvm.load %6 : !llvm.ptr -> i64
      %25 = llvm.load %6 : !llvm.ptr -> i64
      %26 = arith.muli %24, %25 : i64
      %27 = arith.remsi %26, %arg2 : i64
      llvm.store %27, %6 : i64, !llvm.ptr
      %28 = llvm.load %8 : !llvm.ptr -> i64
      %29 = arith.constant 2 : i32
      %31 = arith.extsi %29 : i32 to i64
      %30 = arith.divsi %28, %31 : i64
      llvm.store %30, %8 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %32 = llvm.load %3 : !llvm.ptr -> i64
    func.return %32 : i64
  }
  func.func @nCk(%arg0: i64, %arg1: i64, %arg2: !llvm.ptr, %arg3: !llvm.ptr, %arg4: i64) -> i64 {
    %33 = arith.constant 0 : i32
    %35 = arith.extsi %33 : i32 to i64
    %34 = arith.cmpi slt, %arg1, %35 : i64
    %36 = scf.if %34 -> (i1) {
      %37 = arith.constant true
      scf.yield %37 : i1
    } else {
      %38 = arith.cmpi sgt, %arg1, %arg0 : i64
      scf.yield %38 : i1
    }
    cf.cond_br %36, ^bb6, ^bb7
    ^bb6:
      %39 = arith.constant 0 : i32
      %40 = arith.extsi %39 : i32 to i64
      func.return %40 : i64
    ^bb7:
      cf.br ^bb8
    ^bb8:
    %42 = llvm.getelementptr %arg2[%arg0] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    %41 = llvm.load %42 : !llvm.ptr -> i64
    %44 = llvm.getelementptr %arg3[%arg1] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    %43 = llvm.load %44 : !llvm.ptr -> i64
    %45 = arith.muli %41, %43 : i64
    %46 = arith.remsi %45, %arg4 : i64
    %48 = arith.subi %arg0, %arg1 : i64
    %49 = llvm.getelementptr %arg3[%48] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    %47 = llvm.load %49 : !llvm.ptr -> i64
    %50 = arith.muli %46, %47 : i64
    %51 = arith.remsi %50, %arg4 : i64
    func.return %51 : i64
  }
  func.func @T(%arg0: i64, %arg1: !llvm.ptr, %arg2: !llvm.ptr, %arg3: !llvm.ptr, %arg4: i64) -> i64 {
    %52 = arith.constant 0 : i32
    %53 = arith.extsi %52 : i32 to i64
    %54 = llvm.mlir.constant(1 : i64) : i64
    %55 = llvm.alloca %54 x i64 : (i64) -> !llvm.ptr
    llvm.store %53, %55 : i64, !llvm.ptr
    %56 = arith.constant 1 : i32
    %57 = arith.extsi %56 : i32 to i64
    %58 = llvm.mlir.constant(1 : i64) : i64
    %59 = llvm.alloca %58 x i64 : (i64) -> !llvm.ptr
    llvm.store %57, %59 : i64, !llvm.ptr
    cf.br ^bb9
    ^bb9:
    %60 = llvm.load %59 : !llvm.ptr -> i64
    %61 = arith.constant 2 : i32
    %63 = arith.extsi %61 : i32 to i64
    %62 = arith.cmpi sle, %60, %63 : i64
    cf.cond_br %62, ^bb10, ^bb11
    ^bb10:
      %64 = arith.constant 0 : i32
      %65 = arith.extsi %64 : i32 to i64
      %66 = llvm.mlir.constant(1 : i64) : i64
      %67 = llvm.alloca %66 x i64 : (i64) -> !llvm.ptr
      llvm.store %65, %67 : i64, !llvm.ptr
      cf.br ^bb12
      ^bb12:
      %68 = llvm.load %67 : !llvm.ptr -> i64
      %69 = arith.constant 1 : i32
      %71 = arith.extsi %69 : i32 to i64
      %70 = arith.cmpi sle, %68, %71 : i64
      cf.cond_br %70, ^bb13, ^bb14
      ^bb13:
        %72 = llvm.load %59 : !llvm.ptr -> i64
        %73 = arith.subi %arg0, %72 : i64
        %74 = llvm.load %67 : !llvm.ptr -> i64
        %75 = arith.subi %73, %74 : i64
        %76 = arith.constant 0 : i32
        %78 = arith.extsi %76 : i32 to i64
        %77 = arith.cmpi sge, %75, %78 : i64
        cf.cond_br %77, ^bb15, ^bb16
        ^bb15:
          %79 = arith.constant 0 : i32
          %80 = arith.extsi %79 : i32 to i64
          %81 = llvm.mlir.constant(1 : i64) : i64
          %82 = llvm.alloca %81 x i64 : (i64) -> !llvm.ptr
          llvm.store %80, %82 : i64, !llvm.ptr
          %83 = llvm.load %59 : !llvm.ptr -> i64
          %84 = arith.constant 2 : i32
          %86 = arith.extsi %84 : i32 to i64
          %85 = arith.cmpi eq, %83, %86 : i64
          cf.cond_br %85, ^bb18, ^bb19
          ^bb18:
            %87 = llvm.load %82 : !llvm.ptr -> i64
            %88 = arith.constant 1 : i32
            %90 = arith.extsi %88 : i32 to i64
            %89 = arith.addi %87, %90 : i64
            llvm.store %89, %82 : i64, !llvm.ptr
            cf.br ^bb20
          ^bb19:
            cf.br ^bb20
          ^bb20:
          %91 = llvm.load %67 : !llvm.ptr -> i64
          %92 = arith.constant 1 : i32
          %94 = arith.extsi %92 : i32 to i64
          %93 = arith.cmpi eq, %91, %94 : i64
          cf.cond_br %93, ^bb21, ^bb22
          ^bb21:
            %95 = llvm.load %82 : !llvm.ptr -> i64
            %96 = arith.constant 1 : i32
            %98 = arith.extsi %96 : i32 to i64
            %97 = arith.addi %95, %98 : i64
            llvm.store %97, %82 : i64, !llvm.ptr
            cf.br ^bb23
          ^bb22:
            cf.br ^bb23
          ^bb23:
          %99 = arith.constant 2 : i32
          %101 = arith.extsi %99 : i32 to i64
          %100 = arith.remsi %75, %101 : i64
          %102 = arith.constant 3 : i32
          %104 = arith.extsi %102 : i32 to i64
          %103 = arith.divsi %75, %104 : i64
          %105 = llvm.mlir.constant(1 : i64) : i64
          %106 = llvm.alloca %105 x i64 : (i64) -> !llvm.ptr
          llvm.store %100, %106 : i64, !llvm.ptr
          cf.br ^bb24
          ^bb24:
          %107 = llvm.load %106 : !llvm.ptr -> i64
          %108 = arith.cmpi sle, %107, %103 : i64
          cf.cond_br %108, ^bb25, ^bb26
          ^bb25:
            %109 = arith.constant 3 : i32
            %110 = llvm.load %106 : !llvm.ptr -> i64
            %112 = arith.extsi %109 : i32 to i64
            %111 = arith.muli %112, %110 : i64
            %113 = arith.subi %75, %111 : i64
            %114 = arith.constant 2 : i32
            %116 = arith.extsi %114 : i32 to i64
            %115 = arith.divsi %113, %116 : i64
            %117 = arith.constant 1 : i32
            %118 = llvm.load %106 : !llvm.ptr -> i64
            %120 = arith.extsi %117 : i32 to i64
            %119 = arith.addi %120, %118 : i64
            %121 = arith.addi %119, %115 : i64
            %123 = arith.constant 1 : i32
            %125 = arith.extsi %123 : i32 to i64
            %124 = arith.subi %121, %125 : i64
            %126 = llvm.load %106 : !llvm.ptr -> i64
            %122 = func.call @nCk(%124, %126, %arg1, %arg2, %arg4) : (i64, i64, !llvm.ptr, !llvm.ptr, i64) -> i64
            %127 = llvm.mlir.constant(1 : i64) : i64
            %128 = llvm.alloca %127 x i64 : (i64) -> !llvm.ptr
            llvm.store %122, %128 : i64, !llvm.ptr
            %129 = llvm.load %128 : !llvm.ptr -> i64
            %131 = llvm.load %106 : !llvm.ptr -> i64
            %132 = llvm.getelementptr %arg3[%131] : (!llvm.ptr, i64) -> !llvm.ptr, i64
            %130 = llvm.load %132 : !llvm.ptr -> i64
            %133 = arith.muli %129, %130 : i64
            %134 = arith.remsi %133, %arg4 : i64
            llvm.store %134, %128 : i64, !llvm.ptr
            %135 = llvm.load %128 : !llvm.ptr -> i64
            %137 = llvm.getelementptr %arg1[%121] : (!llvm.ptr, i64) -> !llvm.ptr, i64
            %136 = llvm.load %137 : !llvm.ptr -> i64
            %138 = arith.muli %135, %136 : i64
            %139 = arith.remsi %138, %arg4 : i64
            llvm.store %139, %128 : i64, !llvm.ptr
            %140 = llvm.load %128 : !llvm.ptr -> i64
            %142 = llvm.load %106 : !llvm.ptr -> i64
            %143 = llvm.load %82 : !llvm.ptr -> i64
            %144 = arith.addi %142, %143 : i64
            %145 = llvm.getelementptr %arg1[%144] : (!llvm.ptr, i64) -> !llvm.ptr, i64
            %141 = llvm.load %145 : !llvm.ptr -> i64
            %146 = arith.muli %140, %141 : i64
            %147 = arith.remsi %146, %arg4 : i64
            llvm.store %147, %128 : i64, !llvm.ptr
            %148 = llvm.load %128 : !llvm.ptr -> i64
            %150 = arith.constant 1 : i32
            %152 = arith.extsi %150 : i32 to i64
            %151 = arith.subi %121, %152 : i64
            %153 = llvm.getelementptr %arg1[%151] : (!llvm.ptr, i64) -> !llvm.ptr, i64
            %149 = llvm.load %153 : !llvm.ptr -> i64
            %154 = arith.muli %148, %149 : i64
            %155 = arith.remsi %154, %arg4 : i64
            llvm.store %155, %128 : i64, !llvm.ptr
            %156 = llvm.load %55 : !llvm.ptr -> i64
            %157 = llvm.load %128 : !llvm.ptr -> i64
            %158 = arith.addi %156, %157 : i64
            %159 = arith.remsi %158, %arg4 : i64
            llvm.store %159, %55 : i64, !llvm.ptr
            %160 = llvm.load %106 : !llvm.ptr -> i64
            %161 = arith.constant 2 : i32
            %163 = arith.extsi %161 : i32 to i64
            %162 = arith.addi %160, %163 : i64
            llvm.store %162, %106 : i64, !llvm.ptr
            cf.br ^bb24
          ^bb26:
          cf.br ^bb17
        ^bb16:
          cf.br ^bb17
        ^bb17:
        %164 = llvm.load %67 : !llvm.ptr -> i64
        %165 = arith.constant 1 : i32
        %167 = arith.extsi %165 : i32 to i64
        %166 = arith.addi %164, %167 : i64
        llvm.store %166, %67 : i64, !llvm.ptr
        cf.br ^bb12
      ^bb14:
      %168 = llvm.load %59 : !llvm.ptr -> i64
      %169 = arith.constant 1 : i32
      %171 = arith.extsi %169 : i32 to i64
      %170 = arith.addi %168, %171 : i64
      llvm.store %170, %59 : i64, !llvm.ptr
      cf.br ^bb9
    ^bb11:
    %172 = llvm.load %55 : !llvm.ptr -> i64
    func.return %172 : i64
  }
  func.func @main() -> i32 {
    %173 = arith.constant 100000007 : i32
    %174 = arith.extsi %173 : i32 to i64
    %175 = arith.constant 1000000 : i32
    %176 = arith.extsi %175 : i32 to i64
    %178 = arith.constant 1 : i32
    %180 = arith.extsi %178 : i32 to i64
    %179 = arith.addi %176, %180 : i64
    %181 = arith.constant 8 : i32
    %182 = arith.extsi %181 : i32 to i64
    %177 = func.call @calloc(%179, %182) : (i64, i64) -> !llvm.ptr
    %184 = arith.constant 1 : i32
    %186 = arith.extsi %184 : i32 to i64
    %185 = arith.addi %176, %186 : i64
    %187 = arith.constant 8 : i32
    %188 = arith.extsi %187 : i32 to i64
    %183 = func.call @calloc(%185, %188) : (i64, i64) -> !llvm.ptr
    %190 = arith.constant 3 : i32
    %192 = arith.extsi %190 : i32 to i64
    %191 = arith.divsi %176, %192 : i64
    %193 = arith.constant 4 : i32
    %195 = arith.extsi %193 : i32 to i64
    %194 = arith.addi %191, %195 : i64
    %196 = arith.constant 8 : i32
    %197 = arith.extsi %196 : i32 to i64
    %189 = func.call @calloc(%194, %197) : (i64, i64) -> !llvm.ptr
    %198 = llvm.mlir.zero : !llvm.ptr
    %199 = llvm.icmp "eq" %177, %198 : !llvm.ptr
    %200 = scf.if %199 -> (i1) {
      %201 = arith.constant true
      scf.yield %201 : i1
    } else {
      %202 = llvm.mlir.zero : !llvm.ptr
      %203 = llvm.icmp "eq" %183, %202 : !llvm.ptr
      scf.yield %203 : i1
    }
    %204 = scf.if %200 -> (i1) {
      %205 = arith.constant true
      scf.yield %205 : i1
    } else {
      %206 = llvm.mlir.zero : !llvm.ptr
      %207 = llvm.icmp "eq" %189, %206 : !llvm.ptr
      scf.yield %207 : i1
    }
    cf.cond_br %204, ^bb27, ^bb28
    ^bb27:
      %208 = arith.constant 1 : i32
      func.return %208 : i32
    ^bb28:
      cf.br ^bb29
    ^bb29:
    %209 = arith.constant 1 : i32
    %210 = arith.constant 0 : i32
    %211 = arith.extsi %209 : i32 to i64
    %212 = arith.extsi %210 : i32 to i64
    %213 = llvm.getelementptr %177[%212] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %211, %213 : i64, !llvm.ptr
    %214 = arith.constant 1 : i32
    %215 = arith.extsi %214 : i32 to i64
    %216 = llvm.mlir.constant(1 : i64) : i64
    %217 = llvm.alloca %216 x i64 : (i64) -> !llvm.ptr
    llvm.store %215, %217 : i64, !llvm.ptr
    cf.br ^bb30
    ^bb30:
    %218 = llvm.load %217 : !llvm.ptr -> i64
    %219 = arith.cmpi sle, %218, %176 : i64
    cf.cond_br %219, ^bb31, ^bb32
    ^bb31:
      %221 = llvm.load %217 : !llvm.ptr -> i64
      %222 = arith.constant 1 : i32
      %224 = arith.extsi %222 : i32 to i64
      %223 = arith.subi %221, %224 : i64
      %225 = llvm.getelementptr %177[%223] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %220 = llvm.load %225 : !llvm.ptr -> i64
      %226 = llvm.load %217 : !llvm.ptr -> i64
      %227 = arith.muli %220, %226 : i64
      %228 = arith.remsi %227, %174 : i64
      %229 = llvm.load %217 : !llvm.ptr -> i64
      %230 = llvm.getelementptr %177[%229] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %228, %230 : i64, !llvm.ptr
      %231 = llvm.load %217 : !llvm.ptr -> i64
      %232 = arith.constant 1 : i32
      %234 = arith.extsi %232 : i32 to i64
      %233 = arith.addi %231, %234 : i64
      llvm.store %233, %217 : i64, !llvm.ptr
      cf.br ^bb30
    ^bb32:
    %237 = llvm.getelementptr %177[%176] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    %236 = llvm.load %237 : !llvm.ptr -> i64
    %238 = arith.constant 2 : i32
    %240 = arith.extsi %238 : i32 to i64
    %239 = arith.subi %174, %240 : i64
    %235 = func.call @modpow(%236, %239, %174) : (i64, i64, i64) -> i64
    %241 = llvm.getelementptr %183[%176] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %235, %241 : i64, !llvm.ptr
    llvm.store %176, %217 : i64, !llvm.ptr
    cf.br ^bb33
    ^bb33:
    %242 = llvm.load %217 : !llvm.ptr -> i64
    %243 = arith.constant 0 : i32
    %245 = arith.extsi %243 : i32 to i64
    %244 = arith.cmpi sgt, %242, %245 : i64
    cf.cond_br %244, ^bb34, ^bb35
    ^bb34:
      %247 = llvm.load %217 : !llvm.ptr -> i64
      %248 = llvm.getelementptr %183[%247] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %246 = llvm.load %248 : !llvm.ptr -> i64
      %249 = llvm.load %217 : !llvm.ptr -> i64
      %250 = arith.muli %246, %249 : i64
      %251 = arith.remsi %250, %174 : i64
      %252 = llvm.load %217 : !llvm.ptr -> i64
      %253 = arith.constant 1 : i32
      %255 = arith.extsi %253 : i32 to i64
      %254 = arith.subi %252, %255 : i64
      %256 = llvm.getelementptr %183[%254] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %251, %256 : i64, !llvm.ptr
      %257 = llvm.load %217 : !llvm.ptr -> i64
      %258 = arith.constant 1 : i32
      %260 = arith.extsi %258 : i32 to i64
      %259 = arith.subi %257, %260 : i64
      llvm.store %259, %217 : i64, !llvm.ptr
      cf.br ^bb33
    ^bb35:
    %261 = arith.constant 1 : i32
    %262 = arith.constant 0 : i32
    %263 = arith.extsi %261 : i32 to i64
    %264 = arith.extsi %262 : i32 to i64
    %265 = llvm.getelementptr %189[%264] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %263, %265 : i64, !llvm.ptr
    %266 = arith.constant 1 : i32
    %267 = arith.extsi %266 : i32 to i64
    llvm.store %267, %217 : i64, !llvm.ptr
    cf.br ^bb36
    ^bb36:
    %268 = llvm.load %217 : !llvm.ptr -> i64
    %269 = arith.constant 3 : i32
    %271 = arith.extsi %269 : i32 to i64
    %270 = arith.divsi %176, %271 : i64
    %272 = arith.constant 3 : i32
    %274 = arith.extsi %272 : i32 to i64
    %273 = arith.addi %270, %274 : i64
    %275 = arith.cmpi sle, %268, %273 : i64
    cf.cond_br %275, ^bb37, ^bb38
    ^bb37:
      %277 = llvm.load %217 : !llvm.ptr -> i64
      %278 = arith.constant 1 : i32
      %280 = arith.extsi %278 : i32 to i64
      %279 = arith.subi %277, %280 : i64
      %281 = llvm.getelementptr %189[%279] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %276 = llvm.load %281 : !llvm.ptr -> i64
      %282 = arith.constant 2 : i32
      %284 = arith.extsi %282 : i32 to i64
      %283 = arith.muli %276, %284 : i64
      %285 = arith.remsi %283, %174 : i64
      %286 = llvm.load %217 : !llvm.ptr -> i64
      %287 = llvm.getelementptr %189[%286] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %285, %287 : i64, !llvm.ptr
      %288 = llvm.load %217 : !llvm.ptr -> i64
      %289 = arith.constant 1 : i32
      %291 = arith.extsi %289 : i32 to i64
      %290 = arith.addi %288, %291 : i64
      llvm.store %290, %217 : i64, !llvm.ptr
      cf.br ^bb36
    ^bb38:
    %292 = func.call @T(%176, %177, %183, %189, %174) : (i64, !llvm.ptr, !llvm.ptr, !llvm.ptr, i64) -> i64
    %293 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %294 = llvm.call @printf(%293, %292) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%189) : (!llvm.ptr) -> ()
    func.call @free(%183) : (!llvm.ptr) -> ()
    func.call @free(%177) : (!llvm.ptr) -> ()
    %298 = arith.constant 0 : i32
    func.return %298 : i32
  }
}