f(n) mod 1e9+7 for n=100000. Losing indices mirror-pair to sum 2^n-1, so f(n) = k*(2^n-1)/2 mod M where k = sum over XOR-zero triples (a,b,c) with a+b+c=n of the Hanoi generating-function coefficient. Pure Flow port of the native C solver.
# Project Euler 806: Nim on Towers of Hanoi.
# f(n) mod 1e9+7 for n=100000. Losing indices mirror-pair to sum 2^n-1,
# so f(n) = k*(2^n-1)/2 mod M where k = sum over XOR-zero triples (a,b,c)
# with a+b+c=n of the Hanoi generating-function coefficient.
# Pure Flow port of the native C solver.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
const MOD: i64 = 1000000007
const INV2: i64 = 500000004
function mod_pow(a0: i64, e0: i64) -> i64 {
let mut r: i64 = 1
let mut a: i64 = a0 % MOD
let mut e: i64 = e0
while e != 0 {
if e % 2 == 1 {
r = r * a % MOD
}
a = a * a % MOD
e = e / 2
}
return r
}
# Precompute factorials, inverses, inverse-factorials, powers of 2.
function precompute(nmax: i64, fact_arr: ptr<i64>, inv_arr: ptr<i64>, invfact_arr: ptr<i64>, pow2_arr: ptr<i64>) -> void {
fact_arr[0] = 1
let mut i: i64 = 1
while i <= nmax {
fact_arr[i] = fact_arr[i - 1] * i % MOD
i = i + 1
}
inv_arr[1] = 1
i = 2
while i <= nmax {
inv_arr[i] = MOD - (MOD / i) * inv_arr[MOD % i] % MOD
i = i + 1
}
invfact_arr[nmax] = mod_pow(fact_arr[nmax], MOD - 2)
i = nmax
while i >= 1 {
invfact_arr[i - 1] = invfact_arr[i] * i % MOD
i = i - 1
}
pow2_arr[0] = 1
i = 1
while i <= nmax {
pow2_arr[i] = pow2_arr[i - 1] * 2 % MOD
i = i + 1
}
}
# Open-addressing cache for denom_coeff keyed on packed (a,b,c).
let mut cache_cap: i64 = 0
let mut cache_keys: ptr<i64> = null
let mut cache_vals: ptr<i64> = null
function pack_key(a: i64, b: i64, c: i64) -> i64 {
# a, b, c <= 100005 < 2^17, so 51 bits total.
return (a << 34) | (b << 17) | c
}
function cache_init(cap: i64) -> void {
cache_cap = cap
cache_keys = calloc(cap, 8) as ptr<i64>
cache_vals = calloc(cap, 8) as ptr<i64>
let mut i: i64 = 0
while i < cap {
cache_keys[i] = -1
i = i + 1
}
}
function cache_find(key: i64, out: ptr<i64>) -> i32 {
let mask: i64 = cache_cap - 1
let mut h: i64 = key & mask
while cache_keys[h] != -1 {
if cache_keys[h] == key {
out[0] = cache_vals[h]
return 1
}
h = (h + 1) & mask
}
return 0
}
function cache_insert(key: i64, val: i64) -> void {
let mask: i64 = cache_cap - 1
let mut h: i64 = key & mask
while cache_keys[h] != -1 {
if cache_keys[h] == key {
cache_vals[h] = val
return
}
h = (h + 1) & mask
}
cache_keys[h] = key
cache_vals[h] = val
}
# Coefficient of x^a y^b z^c in 1 / (1 - x^2 - y^2 - z^2 - 2xyz).
function denom_coeff(a: i64, b: i64, c: i64, fact_arr: ptr<i64>, inv_arr: ptr<i64>, invfact_arr: ptr<i64>, pow2_arr: ptr<i64>) -> i64 {
if a < 0 || b < 0 || c < 0 {
return 0
}
if ((a ^ b) % 2 != 0) || ((a ^ c) % 2 != 0) {
return 0
}
let key: i64 = pack_key(a, b, c)
let outp: ptr<i64> = calloc(1, 8) as ptr<i64>
if cache_find(key, outp) != 0 {
let rv: i64 = outp[0]
free(outp)
return rv
}
free(outp)
let mut minabc: i64 = a
if b < minabc {
minabc = b
}
if c < minabc {
minabc = c
}
let mut ans: i64 = 0
let mut i: i64 = a % 2
let mut done: i32 = 0
if i > minabc {
done = 1
}
let mut A: i64 = (a - i) / 2
let mut B: i64 = (b - i) / 2
let mut C: i64 = (c - i) / 2
let mut m: i64 = (a + b + c - i) / 2
let mut term: i64 = pow2_arr[i]
term = term * fact_arr[m] % MOD
term = term * invfact_arr[i] % MOD
term = term * invfact_arr[A] % MOD
term = term * invfact_arr[B] % MOD
term = term * invfact_arr[C] % MOD
while done == 0 {
ans = ans + term
if ans >= MOD {
ans = ans - MOD
}
let i2: i64 = i + 2
if i2 > minabc {
done = 1
} else {
# term_{i+2} / term_i = 4*A*B*C / (m*(i+1)*(i+2))
let mut ratio: i64 = (4 * A) % MOD
ratio = ratio * B % MOD
ratio = ratio * C % MOD
ratio = ratio * inv_arr[m] % MOD
ratio = ratio * inv_arr[i + 1] % MOD
ratio = ratio * inv_arr[i + 2] % MOD
term = term * ratio % MOD
i = i2
A = A - 1
B = B - 1
C = C - 1
m = m - 1
}
}
cache_insert(key, ans)
return ans
}
# Coefficient in Fy with numerator (1+y)(1+x+z-y).
function full_coeff(a: i64, b: i64, c: i64, fact_arr: ptr<i64>, inv_arr: ptr<i64>, invfact_arr: ptr<i64>, pow2_arr: ptr<i64>) -> i64 {
let mut res: i64 = denom_coeff(a, b, c, fact_arr, inv_arr, invfact_arr, pow2_arr)
res = res + denom_coeff(a - 1, b, c, fact_arr, inv_arr, invfact_arr, pow2_arr)
if res >= MOD {
res = res - MOD
}
res = res + denom_coeff(a, b, c - 1, fact_arr, inv_arr, invfact_arr, pow2_arr)
if res >= MOD {
res = res - MOD
}
res = res + denom_coeff(a - 1, b - 1, c, fact_arr, inv_arr, invfact_arr, pow2_arr)
if res >= MOD {
res = res - MOD
}
res = res + denom_coeff(a, b - 1, c - 1, fact_arr, inv_arr, invfact_arr, pow2_arr)
if res >= MOD {
res = res - MOD
}
res = res - denom_coeff(a, b - 2, c, fact_arr, inv_arr, invfact_arr, pow2_arr)
if res < 0 {
res = res + MOD
}
return res
}
function main() -> i32 {
let n: i64 = 100000
if n % 2 != 0 {
printf("%lld\n", 0)
return 0
}
let nmax: i64 = n + 5
let fact_arr: ptr<i64> = calloc(nmax + 1, 8) as ptr<i64>
let inv_arr: ptr<i64> = calloc(nmax + 1, 8) as ptr<i64>
let invfact_arr: ptr<i64> = calloc(nmax + 1, 8) as ptr<i64>
let pow2_arr: ptr<i64> = calloc(nmax + 1, 8) as ptr<i64>
precompute(nmax, fact_arr, inv_arr, invfact_arr, pow2_arr)
cache_init(1 << 16)
# gen_triples: enumerate ordered triples (a,b,c) with a+b+c=n, a xor b xor c = 0.
# For even n, each set bit at position p>=1 gives 3 choices.
let bits: ptr<i64> = calloc(32, 8) as ptr<i64>
let mut nbits: i64 = 0
let mut x: i64 = n
let mut p: i64 = 0
while x != 0 {
if x % 2 == 1 {
bits[nbits] = p
nbits = nbits + 1
}
x = x / 2
p = p + 1
}
let mut total: i64 = 1
let mut bi: i64 = 0
while bi < nbits {
if bits[bi] != 0 {
total = total * 3
}
bi = bi + 1
}
let triples: ptr<i64> = calloc(total * 3, 8) as ptr<i64>
triples[0] = 0
triples[1] = 0
triples[2] = 0
let mut n_triples: i64 = 1
bi = 0
while bi < nbits {
let pp: i64 = bits[bi]
if pp == 0 {
} else {
let v: i64 = 1 << (pp - 1)
let cur: i64 = n_triples
let mut j: i64 = 0
while j < cur {
let ta: i64 = triples[j * 3]
let tb: i64 = triples[j * 3 + 1]
let tc: i64 = triples[j * 3 + 2]
triples[j * 3] = ta + v
triples[j * 3 + 1] = tb + v
triples[j * 3 + 2] = tc
triples[(cur + j) * 3] = ta + v
triples[(cur + j) * 3 + 1] = tb
triples[(cur + j) * 3 + 2] = tc + v
triples[(2 * cur + j) * 3] = ta
triples[(2 * cur + j) * 3 + 1] = tb + v
triples[(2 * cur + j) * 3 + 2] = tc + v
j = j + 1
}
n_triples = 3 * cur
}
bi = bi + 1
}
let mut k: i64 = 0
let mut t: i64 = 0
while t < n_triples {
k = k + full_coeff(triples[t * 3], triples[t * 3 + 1], triples[t * 3 + 2], fact_arr, inv_arr, invfact_arr, pow2_arr)
if k >= MOD {
k = k - MOD
}
t = t + 1
}
let val: i64 = (pow2_arr[n] - 1 + MOD) % MOD
let result: i64 = k * val % MOD * INV2 % MOD
printf("%lld\n", result)
free(fact_arr)
free(inv_arr)
free(invfact_arr)
free(pow2_arr)
free(cache_keys)
free(cache_vals)
free(triples)
free(bits)
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 mod_pow_i64_i64(int64_t a0, int64_t e0);
void precompute_i64_ptr_i64_ptr_i64_ptr_i64_ptr_i64(int64_t nmax, int64_t* fact_arr, int64_t* inv_arr, int64_t* invfact_arr, int64_t* pow2_arr);
int64_t pack_key_i64_i64_i64(int64_t a, int64_t b, int64_t c);
void cache_init_i64(int64_t cap);
int32_t cache_find_i64_ptr_i64(int64_t key, int64_t* out);
void cache_insert_i64_i64(int64_t key, int64_t val);
int64_t denom_coeff_i64_i64_i64_ptr_i64_ptr_i64_ptr_i64_ptr_i64(int64_t a, int64_t b, int64_t c, int64_t* fact_arr, int64_t* inv_arr, int64_t* invfact_arr, int64_t* pow2_arr);
int64_t full_coeff_i64_i64_i64_ptr_i64_ptr_i64_ptr_i64_ptr_i64(int64_t a, int64_t b, int64_t c, int64_t* fact_arr, int64_t* inv_arr, int64_t* invfact_arr, int64_t* pow2_arr);
int32_t main(void);
static const int64_t MOD = 1000000007;
static const int64_t INV2 = 500000004;
/* Module statics */
static int64_t cache_cap = 0;
static int64_t* cache_keys = NULL;
static int64_t* cache_vals = NULL;
int64_t mod_pow_i64_i64(int64_t a0, int64_t e0) {
int64_t r = 1;
int64_t a = FLOW_CHECKED_MOD((a0), (MOD));
int64_t e = e0;
while (e != 0) {
if (FLOW_CHECKED_MOD((e), (2)) == 1) {
r = FLOW_CHECKED_MOD(((r * a)), (MOD));
}
a = FLOW_CHECKED_MOD(((a * a)), (MOD));
e = FLOW_CHECKED_DIV((e), (2));
}
return r;
}
void precompute_i64_ptr_i64_ptr_i64_ptr_i64_ptr_i64(int64_t nmax, int64_t* fact_arr, int64_t* inv_arr, int64_t* invfact_arr, int64_t* pow2_arr) {
fact_arr[0] = 1;
int64_t i = 1;
while (i <= nmax) {
fact_arr[i] = FLOW_CHECKED_MOD(((fact_arr[(i - 1)] * i)), (MOD));
i = (i + 1);
}
inv_arr[1] = 1;
i = 2;
while (i <= nmax) {
inv_arr[i] = (MOD - FLOW_CHECKED_MOD(((FLOW_CHECKED_DIV((MOD), (i)) * inv_arr[FLOW_CHECKED_MOD((MOD), (i))])), (MOD)));
i = (i + 1);
}
invfact_arr[nmax] = mod_pow_i64_i64(fact_arr[nmax], (MOD - 2));
i = nmax;
while (i >= 1) {
invfact_arr[(i - 1)] = FLOW_CHECKED_MOD(((invfact_arr[i] * i)), (MOD));
i = (i - 1);
}
pow2_arr[0] = 1;
i = 1;
while (i <= nmax) {
pow2_arr[i] = FLOW_CHECKED_MOD(((pow2_arr[(i - 1)] * 2)), (MOD));
i = (i + 1);
}
}
int64_t pack_key_i64_i64_i64(int64_t a, int64_t b, int64_t c) {
return ((FLOW_CHECKED_SHL((a), (34)) | FLOW_CHECKED_SHL((b), (17))) | c);
}
void cache_init_i64(int64_t cap) {
cache_cap = cap;
cache_keys = ((int64_t*)(calloc(cap, 8)));
cache_vals = ((int64_t*)(calloc(cap, 8)));
int64_t i = 0;
while (i < cap) {
cache_keys[i] = (-1);
i = (i + 1);
}
}
int32_t cache_find_i64_ptr_i64(int64_t key, int64_t* out) {
int64_t mask = (cache_cap - 1);
int64_t h = (key & mask);
while (cache_keys[h] != (-1)) {
if (cache_keys[h] == key) {
out[0] = cache_vals[h];
return 1;
}
h = ((h + 1) & mask);
}
return 0;
}
void cache_insert_i64_i64(int64_t key, int64_t val) {
int64_t mask = (cache_cap - 1);
int64_t h = (key & mask);
while (cache_keys[h] != (-1)) {
if (cache_keys[h] == key) {
cache_vals[h] = val;
return;
}
h = ((h + 1) & mask);
}
cache_keys[h] = key;
cache_vals[h] = val;
}
int64_t denom_coeff_i64_i64_i64_ptr_i64_ptr_i64_ptr_i64_ptr_i64(int64_t a, int64_t b, int64_t c, int64_t* fact_arr, int64_t* inv_arr, int64_t* invfact_arr, int64_t* pow2_arr) {
if (((a < 0 || b < 0) || c < 0)) {
return 0;
}
if ((FLOW_CHECKED_MOD(((a ^ b)), (2)) != 0 || FLOW_CHECKED_MOD(((a ^ c)), (2)) != 0)) {
return 0;
}
int64_t key = pack_key_i64_i64_i64(a, b, c);
int64_t* outp = (int64_t*)(((int64_t*)(calloc(1, 8))));
if (cache_find_i64_ptr_i64(key, outp) != 0) {
int64_t rv = outp[0];
free(outp);
return rv;
}
free(outp);
int64_t minabc = a;
if (b < minabc) {
minabc = b;
}
if (c < minabc) {
minabc = c;
}
int64_t ans = 0;
int64_t i = FLOW_CHECKED_MOD((a), (2));
int32_t done = 0;
if (i > minabc) {
done = 1;
}
int64_t A = FLOW_CHECKED_DIV(((a - i)), (2));
int64_t B = FLOW_CHECKED_DIV(((b - i)), (2));
int64_t C = FLOW_CHECKED_DIV(((c - i)), (2));
int64_t m = FLOW_CHECKED_DIV(((((a + b) + c) - i)), (2));
int64_t term = pow2_arr[i];
term = FLOW_CHECKED_MOD(((term * fact_arr[m])), (MOD));
term = FLOW_CHECKED_MOD(((term * invfact_arr[i])), (MOD));
term = FLOW_CHECKED_MOD(((term * invfact_arr[A])), (MOD));
term = FLOW_CHECKED_MOD(((term * invfact_arr[B])), (MOD));
term = FLOW_CHECKED_MOD(((term * invfact_arr[C])), (MOD));
while (done == 0) {
ans = (ans + term);
if (ans >= MOD) {
ans = (ans - MOD);
}
int64_t i2 = (i + 2);
if (i2 > minabc) {
done = 1;
} else {
int64_t ratio = FLOW_CHECKED_MOD(((4 * A)), (MOD));
ratio = FLOW_CHECKED_MOD(((ratio * B)), (MOD));
ratio = FLOW_CHECKED_MOD(((ratio * C)), (MOD));
ratio = FLOW_CHECKED_MOD(((ratio * inv_arr[m])), (MOD));
ratio = FLOW_CHECKED_MOD(((ratio * inv_arr[(i + 1)])), (MOD));
ratio = FLOW_CHECKED_MOD(((ratio * inv_arr[(i + 2)])), (MOD));
term = FLOW_CHECKED_MOD(((term * ratio)), (MOD));
i = i2;
A = (A - 1);
B = (B - 1);
C = (C - 1);
m = (m - 1);
}
}
cache_insert_i64_i64(key, ans);
return ans;
}
int64_t full_coeff_i64_i64_i64_ptr_i64_ptr_i64_ptr_i64_ptr_i64(int64_t a, int64_t b, int64_t c, int64_t* fact_arr, int64_t* inv_arr, int64_t* invfact_arr, int64_t* pow2_arr) {
int64_t res = denom_coeff_i64_i64_i64_ptr_i64_ptr_i64_ptr_i64_ptr_i64(a, b, c, fact_arr, inv_arr, invfact_arr, pow2_arr);
res = (res + denom_coeff_i64_i64_i64_ptr_i64_ptr_i64_ptr_i64_ptr_i64((a - 1), b, c, fact_arr, inv_arr, invfact_arr, pow2_arr));
if (res >= MOD) {
res = (res - MOD);
}
res = (res + denom_coeff_i64_i64_i64_ptr_i64_ptr_i64_ptr_i64_ptr_i64(a, b, (c - 1), fact_arr, inv_arr, invfact_arr, pow2_arr));
if (res >= MOD) {
res = (res - MOD);
}
res = (res + denom_coeff_i64_i64_i64_ptr_i64_ptr_i64_ptr_i64_ptr_i64((a - 1), (b - 1), c, fact_arr, inv_arr, invfact_arr, pow2_arr));
if (res >= MOD) {
res = (res - MOD);
}
res = (res + denom_coeff_i64_i64_i64_ptr_i64_ptr_i64_ptr_i64_ptr_i64(a, (b - 1), (c - 1), fact_arr, inv_arr, invfact_arr, pow2_arr));
if (res >= MOD) {
res = (res - MOD);
}
res = (res - denom_coeff_i64_i64_i64_ptr_i64_ptr_i64_ptr_i64_ptr_i64(a, (b - 2), c, fact_arr, inv_arr, invfact_arr, pow2_arr));
if (res < 0) {
res = (res + MOD);
}
return res;
}
int32_t main(void) {
int64_t n = 100000;
if (FLOW_CHECKED_MOD((n), (2)) != 0) {
printf("%lld\n", 0);
return 0;
}
int64_t nmax = (n + 5);
int64_t* fact_arr = (int64_t*)(((int64_t*)(calloc((nmax + 1), 8))));
int64_t* inv_arr = (int64_t*)(((int64_t*)(calloc((nmax + 1), 8))));
int64_t* invfact_arr = (int64_t*)(((int64_t*)(calloc((nmax + 1), 8))));
int64_t* pow2_arr = (int64_t*)(((int64_t*)(calloc((nmax + 1), 8))));
precompute_i64_ptr_i64_ptr_i64_ptr_i64_ptr_i64(nmax, fact_arr, inv_arr, invfact_arr, pow2_arr);
cache_init_i64(FLOW_CHECKED_SHL((1), (16)));
int64_t* bits = (int64_t*)(((int64_t*)(calloc(32, 8))));
int64_t nbits = 0;
int64_t x = n;
int64_t p = 0;
while (x != 0) {
if (FLOW_CHECKED_MOD((x), (2)) == 1) {
bits[nbits] = p;
nbits = (nbits + 1);
}
x = FLOW_CHECKED_DIV((x), (2));
p = (p + 1);
}
int64_t total = 1;
int64_t bi = 0;
while (bi < nbits) {
if (bits[bi] != 0) {
total = (total * 3);
}
bi = (bi + 1);
}
int64_t* triples = (int64_t*)(((int64_t*)(calloc((total * 3), 8))));
triples[0] = 0;
triples[1] = 0;
triples[2] = 0;
int64_t n_triples = 1;
bi = 0;
while (bi < nbits) {
int64_t pp = bits[bi];
if (pp == 0) {
} else {
int64_t v = FLOW_CHECKED_SHL((1), ((pp - 1)));
int64_t cur = n_triples;
int64_t j = 0;
while (j < cur) {
int64_t ta = triples[(j * 3)];
int64_t tb = triples[((j * 3) + 1)];
int64_t tc = triples[((j * 3) + 2)];
triples[(j * 3)] = (ta + v);
triples[((j * 3) + 1)] = (tb + v);
triples[((j * 3) + 2)] = tc;
triples[((cur + j) * 3)] = (ta + v);
triples[(((cur + j) * 3) + 1)] = tb;
triples[(((cur + j) * 3) + 2)] = (tc + v);
triples[(((2 * cur) + j) * 3)] = ta;
triples[((((2 * cur) + j) * 3) + 1)] = (tb + v);
triples[((((2 * cur) + j) * 3) + 2)] = (tc + v);
j = (j + 1);
}
n_triples = (3 * cur);
}
bi = (bi + 1);
}
int64_t k = 0;
int64_t t = 0;
while (t < n_triples) {
k = (k + full_coeff_i64_i64_i64_ptr_i64_ptr_i64_ptr_i64_ptr_i64(triples[(t * 3)], triples[((t * 3) + 1)], triples[((t * 3) + 2)], fact_arr, inv_arr, invfact_arr, pow2_arr));
if (k >= MOD) {
k = (k - MOD);
}
t = (t + 1);
}
int64_t val = FLOW_CHECKED_MOD((((pow2_arr[n] - 1) + MOD)), (MOD));
int64_t result = FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((k * val)), (MOD)) * INV2)), (MOD));
printf("%lld\n", result);
free(fact_arr);
free(inv_arr);
free(invfact_arr);
free(pow2_arr);
free(cache_keys);
free(cache_vals);
free(triples);
free(bits);
return 0;
}