B. Identify the Operations - 找规律

B. Identify the Operations

time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

We start with a permutation a1,a2,…,ana1,a2,…,an and with an empty array bb. We apply the following operation kk times.

On the ii-th iteration, we select an index titi (1≤ti≤n−i+11≤ti≤n−i+1), remove atiati from the array, and append one of the numbers ati−1ati−1 or ati+1ati+1 (if ti−1ti−1 or ti+1ti+1 are within the array bounds) to the right end of the array bb. Then we move elements ati+1,…,anati+1,…,an to the left in order to fill in the empty space.

You are given the initial permutation a1,a2,…,ana1,a2,…,an and the resulting array b1,b2,…,bkb1,b2,…,bk. All elements of an array bb are distinct. Calculate the number of possible sequences of indices t1,t2,…,tkt1,t2,…,tk modulo 998244353998244353.

Input

Each test contains multiple test cases. The first line contains an integer tt (1≤t≤1000001≤t≤100000), denoting the number of test cases, followed by a description of the test cases.

The first line of each test case contains two integers n,kn,k (1≤k<n≤2000001≤k<n≤200000): sizes of arrays aa and bb.

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤n1≤ai≤n): elements of aa. All elements of aa are distinct.

The third line of each test case contains kk integers b1,b2,…,bkb1,b2,…,bk (1≤bi≤n1≤bi≤n): elements of bb. All elements of bb are distinct.

The sum of all nn among all test cases is guaranteed to not exceed 200000200000.

Output

For each test case print one integer: the number of possible sequences modulo 998244353998244353.

Example

input

Copy

3
5 3
1 2 3 4 5
3 2 5
4 3
4 3 2 1
4 3 1
7 4
1 4 7 3 6 2 5
3 2 4 5

output

Copy

2
0
4

Note

Let's denote as a1a2…aiai+1––––…an→a1a2…ai−1ai+1…an−1a1a2…aiai+1_…an→a1a2…ai−1ai+1…an−1 an operation over an element with index ii: removal of element aiai from array aa and appending element ai+1ai+1 to array bb.

In the first example test, the following two options can be used to produce the given array bb:

  • 123–45→12–35→125–→12123_45→12_35→125_→12; (t1,t2,t3)=(4,3,2)(t1,t2,t3)=(4,3,2);
  • 123–45→12–35→235–→15123_45→12_35→235_→15; (t1,t2,t3)=(4,1,2)(t1,t2,t3)=(4,1,2).

In the second example test, it is impossible to achieve the given array no matter the operations used. That's because, on the first application, we removed the element next to 44, namely number 33, which means that it couldn't be added to array bb on the second step.

In the third example test, there are four options to achieve the given array bb:

  • 1473–625→14362–5→14–325→4325–→4351473_625→14362_5→14_325→4325_→435;
  • 1473–625→14362–5→14–325→1425–→1451473_625→14362_5→14_325→1425_→145;
  • 1473–625→14732–5→14–725→4725–→4751473_625→14732_5→14_725→4725_→475;
  • 1473–625→14732–5→14–725→1425–→1451473_625→14732_5→14_725→1425_→145;
  • ======================================================================

手玩一下样例,发现几个规律

要想删除当前pos位置的数字,pos两侧的数字要么是和b数组无关的,要么是已经被放进c里面的数字,所以考虑打标记的做法,对于b中有的,先事先打好标记,放进去一个我们就取消这个数字的标记,每次判断的时候,看两侧的可能性,也就是未被标记数字的个数即可

#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;

# define mod 998244353
int book[200000+10];
int a[200000+10],b[200000+10],pos[200000+10];
int main()
{

   int t;
   cin>>t;
   while(t--)
   {
       int n,m;
       cin>>n>>m;

       for(int i=1;i<=n;i++)
       {
           book[i]=0;
           cin>>a[i];
       }

       for(int i=1;i<=m;i++)
       {
           cin>>b[i];
           book[b[i]]=1;
       }

       for(int i=1;i<=n;i++)
       {
           pos[a[i]]=i;
       }
       ll ans=1;
       for(int i=1;i<=m;i++)
       {
           if(pos[b[i]]==1)
           {
               if(book[a[2]]==1)
               {
                   ans=0;
                   break;
               }

               book[b[i]]=0;
           }
           else if(pos[b[i]]==n)
           {
               if(book[a[n-1]]==1)
               {
                   ans=0;
                   break;
               }

               book[b[i]]=0;
           }
           else
           {

               ll fuck=2;

               if(book[a[pos[b[i]]-1]]==1)
                fuck--;
               if(book[a[pos[b[i]]+1]]==1)
                fuck--;

               ans=ans*fuck%mod;

               if(ans==0)
                break;

               book[a[pos[b[i]]]]=0;
           }
       }

       cout<<ans<<endl;


   }


    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秦三码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值