思路:推一下式子:
等价于
那么其他情况下 参考代码如下(代码和上面思路不完全一致,仅供参考)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int long long
const int N=200010;
const int P=998244353;
int a[N];
void solve(){
int n;cin>>n;
ll ans=0;
bool f1=0,f2=0;
for(int i=1;i<=n;i++){
cin>>a[i];
if(f1){
if(a[i]==0 || a[i]==1 || !f2){
ans=ans+a[i];
if(!f2 && ans>1){
f2=1;
}
ans=ans%P;
}
else{
ans=((ans%P)*(a[i]%P))%P;
}
}
else{
ans=ans+a[i];
if(ans!=0){
f1=1;
if(ans>1){
f2=1;
}
}
ans=ans%P;
}
}
cout<<ans<<endl;
}
signed main(){
int T;cin>>T;
while(T--){
solve();
}
return 0;
}