考虑将其拆分,。
然后将 反转,也变成求 , 变成求相同,方便一些。
然后考虑求:对于每个 ,求与之匹配的 子序列个数。
方便起见,我们正反跑两遍,分三类讨论(常数要大一些,不过好写很多)。
- 全在 前面。
- 全在 后面。
- 在左边, 在右边。
先考虑第一种,其余的同理可以知道怎么做。
全在左边时产生贡献当且仅当其与 分在了同一个段,也就是 的位置与 在同一个段,假设 在位置 ,则对答案的贡献是 。
然后用 把 存下来,这个是容易的,出现 时与出现 时分开讨论。
#include<bits/stdc++.h>
using namespace std;
namespace Fread {
const int SIZE=1<<21;char buf[SIZE],*S,*T;
inline char getchar() {if(S==T){T=(S=buf)+fread(buf,1,SIZE,stdin);if(S==T)return '\n';}return *S++;}
}
namespace Fwrite {
const int SIZE=1<<21;
char buf[SIZE],*S=buf,*T=buf+SIZE;
inline void flush(){fwrite(buf,1,S-buf,stdout);S=buf;}
inline void putchar(char c){*S++=c;if(S==T)flush();}
struct POPOSSIBLE{~POPOSSIBLE(){flush();}}ztr;
}
#define getchar Fread :: getchar
#define putchar Fwrite :: putchar
namespace Fastio{
struct Reader{
template<typename T>
Reader& operator >> (T& x) {
char c=getchar();T f=1;
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}x=0;
while(c>='0'&&c<='9'){x=x*10+(c-'0');c=getchar();}x*=f;
return *this;
}
Reader(){}
}cin;
struct Writer{
template<typename T>
Writer& operator << (T x) {
if(x==0){putchar('0');return *this;}
static int sta[45];int top=0;
while(x){sta[++top]=x%10;x/=10;}
while(top){putchar(sta[top]+'0');--top;}
return *this;
}
Writer& operator << (char c) {putchar(c);return *this;}
Writer(){}
}cout;
}
#define endl '\n'
#define cin Fastio :: cin
#define cout Fastio :: cout
typedef unsigned long long ull;
const int mod=1e9+7;
struct modint {
int val;
static int norm(const int& x) { return x < 0 ? x + mod : x; }
inline int Mod(const int& x){return x>=mod?x-mod:x;}
modint() : val(0) {}
modint(const int& m) : val(m) {}
modint& operator-=(const modint& o) { return val = norm(val - o.val), *this; }
modint& operator+=(const modint& o) { return val = Mod(val + o.val), *this; }
modint& operator*=(const modint& o) { return val = static_cast<int>(1ll * val * o.val % mod), *this; }
modint operator+(const modint& o) { return Mod(val+o.val); }
modint operator*(const modint& o) const { return 1ll*val*o.val%mod; }
inline void Mul(){val=Mod(val<<1);}
};
const modint inv2=(mod+1)/2;
const int N=1e5+10;
modint sum1,sum2,res,ans,pref,preg,suff,sufg;
bitset < 100000001 > f,g;
int n,op;
inline modint expow(modint x,int y){
modint res=1;
for(;y;y>>=1){if(y&1) res*=x;x*=x;}
return res;
}
int main(){
cin>>n>>op;register ull f1,f2,g1,g2,x1,y1,x2,y2,z1,z2,nowf,nowg;
if(op==0){for(int i=1;i<=n;++i){cin>>x1>>y1;f[i]=x1;g[i]=!y1;}}
else {
cin>>x1>>y1>>z1>>f1>>f2;
cin>>x2>>y2>>z2>>g1>>g2;
int cnt=0;
while(cnt+64<=n){
nowf=f2*y1+f1*x1+z1;
nowg=g2*y2+g1*x2+z2;
f1=f2;f2=nowf;g1=g2;g2=nowg;
for(int i=1;i<=64;++i){
++cnt;
f[cnt]=nowf&1;
g[cnt]=(nowg&1)^1;
nowf>>=1;nowg>>=1;
}
}
nowf=f2*y1+f1*x1+z1;
nowg=g2*y2+g1*x2+z2;
f1=f2;f2=nowf;g1=g2;g2=nowg;
for(int i=cnt+1;i<=n;++i){
f[i]=nowf&1;
g[i]=(nowg&1)^1;
nowf>>=1;nowg>>=1;
}
}
modint p1=expow(2,n),p2=inv2;
for(int i=n;i;--i){
p1*=inv2;p2.Mul();
f[i]?sum1+=p2,suff+=p2:res+=sum1;
g[i]?sum2+=p2,sufg+=p2:res+=sum2;
ans+=(f[i]==g[i])?res*p1:0;
}
sum1=sum2=res=0;
p1=expow(2,n);p2=inv2;
for(int i=1;i<=n;++i){
p1*=inv2;p2.Mul();
f[i]?res+=sum1,suff-=p1:sum1+=p2;
g[i]?res+=sum2,sufg-=p1:sum2+=p2;
ans+=(f[i]==g[i])?res*p1+suff*pref+sufg*preg:0;
(!f[i])?pref+=p2:0;
(!g[i])?preg+=p2:0;
}
cout<<ans.val<<endl;
return 0;
}