Guest User

Untitled

a guest
Sep 19th, 2025
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.44 KB | Source Code | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. typedef unsigned long long ull;
  5. #define MOD 1000000007
  6. #define F first
  7. #define S second
  8. #define PB push_back
  9. #define MP make_pair
  10. #define FOR(i,a,b) for(int i=a;i<b;i++)
  11. #define FORll(i,a,b) for(ll i=a;i<b;i++)
  12. #define yes cout<<"YES\n";
  13. #define no cout<<"NO\n";
  14. #define all(x) begin(x), end(x)
  15. #define sz(x) (int)(x).size()
  16. #define fast_io ios::sync_with_stdio(0);cin.tie(0);
  17. #define debug(x) for(auto k:x) cout<<k<<" ";cout<<"\n";
  18. #define int long long
  19. #define double long double
  20. #define INF LLONG_MAX
  21. #define endl "\n"
  22. int gcd(int a,int b){if(b==0)return a;else return gcd(b,a%b);}
  23. int lcm(int a,int b){return a*b/gcd(a,b);}
  24. int power(int x, unsigned int y, unsigned int M)
  25. {
  26.     if (y == 0)
  27.         return 1;
  28.     int p = power(x, y / 2, M) % M;
  29.     p = (p * p) % M;
  30.     return (y % 2 == 0) ? p : (x * p) % M;
  31. }
  32. int modInverse(int A, int M)
  33. {
  34.     int g = gcd(A, M);
  35.     if (g != 1)
  36.         return -1;
  37.     else {
  38.        return power(A, M - 2, M);
  39.     }
  40. }
  41. struct TreeNode{
  42.     int val;
  43.     TreeNode* left;
  44.     TreeNode* right;
  45.     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  46. };
  47. //DSU Data Structure for MST
  48. class DSU{
  49. private:
  50.     vector<int> par;
  51.     vector<int> siz;
  52. public:
  53.     DSU(int n){
  54.         par.resize(n+1);
  55.         siz.resize(n+1);
  56.         for(int i=1;i<=n;i++){
  57.             par[i] = i;
  58.             siz[i] = 1;
  59.         }
  60.     }
  61.     int get_leader(int u){
  62.         if(par[u]==u) return u;
  63.         // path compression -> O(n) to O(1) amortized
  64.         return par[u] = get_leader(par[u]);
  65.     }
  66.     bool merge(int u,int v){
  67.         u = get_leader(u);
  68.         v = get_leader(v);
  69.         if(u==v) return false;
  70.         //union by rank
  71.         if(siz[u]<siz[v]){
  72.             siz[v] += siz[u];
  73.             par[u] = v;
  74.         }
  75.         else{
  76.             siz[u] += siz[v];
  77.             par[v] = u;
  78.         }
  79.         return true;
  80.     }
  81. };
  82. // --- code starts here ---
  83. //to reverse sort use greater<int>()
  84. //use "\n" instead of endl
  85. //use INF , -INF for initialising
  86. signed main(){
  87.     //prob D
  88.     fast_io;
  89.     int t = 1;
  90.     cin>>t;
  91.     while(t--){
  92.         int n;
  93.         cin>>n;
  94.         vector<int> a(n);
  95.         for(auto &x: a){cin>>x;}
  96.         // dp[i][j] -> num of good subsequences ending at index i such that
  97.         // max value of any element in this subseq is j
  98.        
  99.         vector<vector<int>> dp(n,vector<int>(n+1,0));
  100.        
  101.         FOR(i, 0, n){
  102.             dp[i][a[i]] = 1;
  103.         }
  104.        
  105.        
  106.         FOR(i, 0, n) {
  107.             FOR(j, 1, n + 1) {
  108.                 FOR(k, i + 1, n) {
  109.                     if (dp[i][j] == 0) continue;
  110.  
  111.                     if (a[k] >= a[i]) {
  112.                         int new_max = max(j, a[k]);
  113.                         dp[k][new_max] = (dp[k][new_max] + dp[i][j]) % MOD;
  114.                     } else {
  115.                         if (j == a[i] || a[k] >= j) {
  116.                             int new_max = max(j, a[k]);
  117.                             dp[k][new_max] = (dp[k][new_max] + dp[i][j]) % MOD;
  118.                         }
  119.                     }
  120.                 }
  121.             }
  122.         }
  123.         int ans = 1;
  124.         FOR(i, 0, n) {
  125.             FOR(j, 1, n + 1) {
  126.                 // cout << i << " " << j << " : " << dp[i][j] << endl;
  127.                 ans = (ans + dp[i][j]) % MOD;
  128.             }
  129.         }
  130.         cout << ans << endl;
  131.        
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment