Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- const long long mod = 1e9 + 7;
- void solve() {
- ll n, m;
- cin >> n >> m;
- if (m > n) {
- cout << 0 << endl;
- return;
- }
- ll mx = 1;
- map<ll, ll> last;
- set<ll> st;
- vector<ll> a(n + 1), b(m + 1);
- for (int i = 1; i <= n; i++) {
- cin >> a[i];
- last[a[i]] = i;
- mx = max(mx, a[i]);
- st.insert(a[i]);
- }
- vector<ll> unique;
- for (auto curr : st) {
- unique.push_back(curr);
- }
- reverse(unique.begin(), unique.end());
- for (int i = 1; i <= m; i++) {
- cin >> b[i];
- }
- if (mx > b[1]) {
- cout << 0 << endl;
- return;
- }
- for (int i = 1; i <= m; i++) {
- if (last.find(b[i]) == last.end()) {
- cout << 0 << endl;
- return;
- }
- }
- ll ptr = 0, ans = 1;
- for (int i = 1; i < m; i++) {
- ll cmax = 0;
- while (unique[ptr] > b[i + 1]) {
- cmax = max(cmax, last[unique[ptr]]);
- ptr++;
- }
- if (cmax > last[unique[ptr]]) {
- cout << 0 << endl;
- return;
- }
- ans = ((ans * (last[unique[ptr]] - cmax)) % mod + mod) % mod;
- }
- cout << ans << endl;
- }
- signed main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- int t;
- cin >> t;
- while (t--) {
- solve();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement