vector<int>berlekamp_massey(constvector<int>&a){vector<int>v,last;// v is the answer, 0-based, p is the moduleintk=-1,delta=0;for(inti=0;i<(int)a.size();i++){inttmp=0;for(intj=0;j<(int)v.size();j++)tmp=(tmp+(longlong)a[i-j-1]*v[j])%p;if(a[i]==tmp)continue;if(k<0){k=i;delta=(a[i]-tmp+p)%p;v=vector<int>(i+1);continue;}vector<int>u=v;intval=(longlong)(a[i]-tmp+p)*power(delta,p-2)%p;if(v.size()<last.size()+i-k)v.resize(last.size()+i-k);(v[i-k-1]+=val)%=p;for(intj=0;j<(int)last.size();j++){v[i-k+j]=(v[i-k+j]-(longlong)val*last[j])%p;if(v[i-k+j]<0)v[i-k+j]+=p;}if((int)u.size()-i<(int)last.size()-k){last=u;k=i;delta=a[i]-tmp;if(delta<0)delta+=p;}}for(auto&x:v)x=(p-x)%p;v.insert(v.begin(),1);returnv;// $\forall i, \sum_{j = 0} ^ m a_{i - j} v_j = 0$}
vector<int>solve_sparse_equations(constvector<tuple<int,int,int>>&A,constvector<int>&b){intn=(int)b.size();// 0-basedvector<vector<int>>f({b});for(inti=1;i<2*n;i++){vector<int>v(n);auto&u=f.back();for(auto[x,y,z]:A)// [x, y, value]v[x]=(v[x]+(longlong)u[y]*z)%p;f.push_back(v);}vector<int>w(n);mt19937gen;for(auto&x:w)x=uniform_int_distribution<int>(1,p-1)(gen);vector<int>a(2*n);for(inti=0;i<2*n;i++)for(intj=0;j<n;j++)a[i]=(a[i]+(longlong)f[i][j]*w[j])%p;autoc=berlekamp_massey(a);intm=(int)c.size();vector<int>ans(n);for(inti=0;i<m-1;i++)for(intj=0;j<n;j++)ans[j]=(ans[j]+(longlong)c[m-2-i]*f[i][j])%p;intinv=power(p-c[m-1],p-2);for(inti=0;i<n;i++)ans[i]=(longlong)ans[i]*inv%p;returnans;}