local ffi = require 'ffi' local M = ffi.load 'gmp' ffi.cdef [[ typedef struct { int a; int s; long * d; } _z; typedef _z zt[1]; typedef const _z * spt; typedef _z * pt; void __gmpz_init(pt); void __gmpz_init_set_d(pt, double); void __gmpz_add(pt, spt, spt); int __gmpz_cmp(spt, spt); void __gmpz_mod(pt, spt, spt); void __gmpz_mul(pt, spt, spt); void __gmpz_tdiv_qr(pt, pt, spt, spt); int __gmp_printf(const char *, ...); int __gmp_scanf(const char *, ...); ]] M.__gmp_printf("%Zd\n", (function (f, g, n) local t,c,d,r,one,two,td = g(),g(),g(),g(),g(1),g(2),g(1000000007) for _=1,n do f(t,c,d,r,one,two) end M.__gmpz_mod(t,t,td) return t end)(function (t, c, d, r, one, two) M.__gmp_scanf("%Zd %Zd",c,d) M.__gmpz_tdiv_qr(c,r,c,two) if M.__gmpz_cmp(r,one)==0 then M.__gmpz_add(c,c,one) end M.__gmpz_mul(c,c,d) M.__gmpz_add(t,t,c) end, function (n) local cd = ffi.new("zt") if n~=nil then M.__gmpz_init_set_d(cd,n) else M.__gmpz_init(cd) end return cd end, io.stdin:read("*n")))