#include #include #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #define PI acos(-1) #define all(v) (v).begin(),(v).end() #define fi first #define se second #define endll "\n" using namespace std; using namespace atcoder; using ll = long long; template constexpr inline void input(vector &v){ for(int i=0;i> v[i]; } template constexpr inline void input(vector &v,vector &u){ for(int i=0;i> v[i] >> u[i]; } template constexpr inline void input(vector &v,vector &u,vector &t){ for(int i=0;i> v[i] >> u[i] >> t[i]; } template constexpr inline void input(vector> &v){ for(int i=0;i> v[i][j]; } } template constexpr inline void input_graph(vector> &G,int inputcount,const bool isdirect = false,const bool indexed = 1){ T a,b; for(int i=0;i> a >> b;a -= indexed;b -= indexed; G[a].emb(b); if(!isdirect) G[b].emb(a); } } template constexpr inline void output(vector &v,bool space = true){ if(space){ for(int i=0;i constexpr inline void output(vector &v,vector &u){ for(int i=0;i constexpr inline void output(vector &v,vector &u,vector &t){ for(int i=0;i constexpr inline void output(vector> &v){ for(int i=0;i constexpr inline bool on(T n,T i){ return n&(1< constexpr inline T ceil(T x,S y){ return (x+y-1)/y; } template constexpr bool isprime(T x){ for(T i=2;i*i<=x;i++){ if(x%i == 0) return false; } return true; } vector isprime_format(int n){ vector P(n+1,1);P[0] = P[1] = 1; for(int i=2;i*i<=n;i++){ if(!P[i]) continue; for(int j=i+i;j<=n;j+=i) P[j] = false; } return P; } template vector dijkstra(T N,vector st,vector>> G){ T fn,fp,i; priority_queue,vector>,greater<>> q; vector D(N,-1); for(i=0;i D[fp]+G[fp][i].se){ D[G[fp][i].fi] = D[fp]+G[fp][i].se; q.push(mpa(D[G[fp][i].fi],G[fp][i].fi)); } } } return D; } template vector dijkstra(T N,T st,vector>> G){ return dijkstra(N,vector({st}),G); } template class WarshallFloyd{ T N,inf; vector> D; vector> prev; bool isdirect; void setting(){ for(T k=0;k D[i][k]+D[k][j]){ D[i][j] > D[i][k]+D[k][j]; prev[i][j] = prev[k][j]; } } } } } public: WarshallFloyd(T N,vector &u,vector &v,vector &c,T inf,bool isdirect){ this->N = N; this->inf = inf; this->isdirect = isdirect; assert(u.size() == v.size()); vector>(N,vector(N,inf)).swap(D); vector>(N,vector(N)).swap(prev); for(T i=0;i> D,T inf,bool isdirect){ this->N = D.size(); this->inf = inf; this->D = D; this->isdirect = isdirect; vector>(N,vector(N)).swap(prev); for(T i=0;i D[i][s]+c+D[t][j]){ D[i][j] = D[i][s]+c+D[t][j]; prev[i][j] = prev[t][j]; } } if(!isdirect && D[i][t] != inf && D[s][j] != inf){ if(D[i][j] > D[i][t]+c+D[s][j]){ D[i][j] = D[i][t]+c+D[s][j]; prev[i][j] = prev[s][j]; } } } } } T at(T i,T j){ return D[i][j]; } vector Path(T s,T t){ vector ret; ret.emb(t); while(t != s) ret.emb(t=prev[s][t]); reverse(all(ret)); return ret; } bool negative_cycle(){ for(T i=0;i> Graph(){ return D; } }; template class mat{ vector> V; public: constexpr mat(){} constexpr mat(int N,int M){ vector>(N,vector(M)).swap(this->V); } constexpr mat(vector> &v){ this->V = v; } constexpr int height(){return V.size();} constexpr int width(){return V[0].size();} constexpr T &val(int a,int b){return V[a][b];} constexpr vector &val(int a){return V[a];} constexpr vector> &val(){return V;} //ret(mat[i][j],elem(a[i][k],b[k][j])) constexpr mat calc(mat &b,function ret = [](T x,T y){return x+y;},function elem = [](T x,T y){return x*y;})const{ vector> c(V.size(),vector(b.width())); for(int i=0;i ret = [](T x,T y){return x+y;},function elem = [](T x,T y){return x*y;}) const { mat x = *this,z; while(y){ if(y&1){ if(z.height() == 0) z = x; else z = z.calc(x,ret,elem); } x = x.calc(x,ret,elem); y >>= 1; } return z; } }; using mint = modint1000000007; ll solve_main(ll K,const vector &D,const vector &L){ vector mod7({1,3,2,6,4,5}); vector> dp(6,vector(7));//dp[j%6][k%7] -> これまでの桁数がj,数がk vector> newdp; dp[0][0] = 1; //後ろからDP for(ll i=K-1;i>=0;i--){ newdp = dp; for(ll j=0;j<6;j++){ //これまでの桁数%6をjとして、足される数(mod7)を求める -> now ll now = 0; for(ll k=0;k &D,const vector &L){ mint ans = 0; vector mod7({1,3,2,6,4,5}); for(ll b=0;b<(1<=0;i--){ if(!on(b,i)) continue; now = (now+(L[i]/6)*21*D[i])%7; for(ll j=0;j> K; vector D(K),L(K);input(D,L); //cout << solve_bitfullsearch(K,D,L) << endll; cout << solve_main(K,D,L) << endll; }