Sanath12345

Untitled

Sep 15th, 2021
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. //Problem link :
  2. //CREDITS : sanath kumar
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5. #define ll long long int
  6. #define ld long double
  7. #define mod 1000000007
  8. #define inf 1e9
  9. #define endl "\n"
  10. #define pb push_back
  11. #define vi vector<ll>
  12. #define vs vector<string>
  13. #define pii pair<ll,ll>
  14. #define ump unordered_map
  15. #define mp make_pair
  16. #define pq_max priority_queue<ll>
  17. #define pq_min priority_queue<ll,vector<ll>,greater<ll>>
  18. #define all(v) v.begin(),v.end()
  19. #define ff first
  20. #define ss second
  21. #define mid(l,r) (l+(r-l))/2
  22. #define bitc(x) __builtin_popcount(x)
  23. #define read(a,n) for(int i=0;i<n;i++) cin>>a[i];
  24. #define loop(i,a,b) for(int i=(a);i<=(b);i++)
  25. #define looprev(i,a,b) for(int i=(a);i>=(b);i--)
  26. #define iter(c,it) for(__typeof(c.begin()) it=c.begin();it!=c.end();it++)
  27. #define boost ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
  28. #define log(args...) {string _s=#args;replace(_s.begin(),_s.end(),',',' ');stringstream _ss(_s);istream_iterator<string> _it(_ss);err(_it,args);}
  29. #define logarr(arr,a,b) for(int z=(a);z<=(b);z++) cout<<(arr[z])<<" ";cout<<endl;
  30. template<typename T> void shiftr(T *arr,int n){for(int i=n;i>=1;i--) arr[i]=arr[i-1];arr[0]=0;}
  31. template<typename T> T gcd(T a,T b) {if(b==0) return a; return gcd(b,a%b);}
  32. template<typename T> T lcm(T a,T b) {return a*(b/gcd(a,b));}
  33. template<class T> T add(T a,T b) {return ((a%mod)+(b%mod))%mod;}
  34. template<class T> T sub(T a,T b) {return ((a%mod)-(b%mod)+mod)%mod;}
  35. template<class T> T mul(T a,T b) {return ((a%mod)*(b%mod)*(1LL))%mod;}
  36. template<class T> void debug(set<T> S){cout<<"[ ";for(auto i:S) cout<<i<<" ";cout<<"] "<<endl;}
  37. template<class T> void debug(vector<ll> v){cout<<"[ ";for(auto i:v) cout<<i<<" ";cout<<"] "<<endl;}
  38. template<class T> void debug(multiset<T> S){cout<<"[ ";for(auto i:S) cout<<i<<" ";cout<<"] "<<endl;}
  39. template<class T> void debug(unordered_set<T> S){cout<<"[ ";for(auto i:S) cout<<i<<" ";cout<<"] "<<endl;}
  40. template<class T,class X> void debug(T *arr,X s,X e){cout<<"[ ";loop(i,s,e) cout<<arr[i]<<" ";cout<<"] "<<endl;}
  41. template<class T,class V> void debug(pair<T,V> p){cout<<"{";cout<<p.ff<<" "<<p.ss<<"}"<<endl;}
  42. template<class T,class V> void debug(vector<pair<T,V>> v){cout<<"[ "<<endl;for(auto i:v) debug(i);cout<<"]"<<endl;}
  43.  
  44. void err(istream_iterator<string> it){}
  45. template<typename T,typename... Args>
  46. void err(istream_iterator<string> it,T a,Args... args){
  47. cout<<*it<<" = "<<a<<endl;
  48. err(++it,args...);
  49. }
  50. ll *getfactorial(){
  51. int m=1e6;
  52. ll *fact=new ll[m+1];
  53. fact[0]=1;
  54. loop(i,1,m){
  55. fact[i]=(fact[i-1]*i)%mod;
  56. }
  57. // time complexity O(n)
  58. return fact;
  59. }
  60. vi *getfactors(){
  61. int n=2e5;
  62. vi *factors=new vi[n+5];
  63. loop(i,1,n){
  64. for(int j=i;j<=n;j+=i){
  65. factors[j].pb(i);
  66. }
  67. }
  68. // time complexity O(nlogn)
  69. return factors;
  70. }
  71. ll power(ll a,ll b,ll m=mod){
  72. if(b==0) return 1;
  73. ll smallans=power(a,b/2,m);
  74. ll ans=(smallans*smallans)%m;
  75. if(b%2==1){
  76. ans=(ans*a)%m;
  77. }
  78. return ans;
  79. // time complexity O(logb)
  80. }
  81. bool *isprime(){
  82. int m=1e6;
  83. bool *p=new bool[m];
  84. loop(i,0,m) p[i]=true;
  85. p[0]=false;
  86. p[1]=false;
  87. loop(i,2,sqrt(m)){
  88. for(int j=2*i;j<=m;j+=i){
  89. p[j]=false;
  90. }
  91. }
  92. // time complexity O(nlogn)
  93. return p;
  94. }
  95. ll nCr(ll n,ll r,int m=mod){
  96. if(n<r) return 0;
  97. ll *fac=getfactorial();
  98. ll ans=fac[n];
  99. ans=(ans*power(fac[n-r],m-2))%m;
  100. ans=(ans*power(fac[r],m-2))%m;
  101. return ans;
  102. }
  103.  
  104. //==================START==================
  105. ll get(int a,int b){
  106. return (1e5-max(a,b)+1);
  107. }
  108. int order(int *A,int n){
  109. int tag=0;
  110. loop(i,1,n-1){
  111. if(A[i]<A[i-1]){
  112. tag=1;
  113. break;
  114. }
  115. }
  116. if(tag==0) return -1; // inc
  117. tag=0;
  118. loop(i,1,n-1){
  119. if(A[i]>A[i-1]){
  120. tag=1;
  121. break;
  122. }
  123. }
  124. if(tag==0) return -2; // dec
  125. if(A[0]>A[1]) return -3;
  126. int i=0,z=0,p;
  127. while(i<n-1){
  128. while(i+1<n and A[i+1]>=A[i]) i++;
  129. if(i+1<n and A[i+1]<A[i]){
  130. p=i;
  131. z++;
  132. while(i+1<n and A[i+1]<A[i]) i++;
  133. }
  134. if(i!=n-1) return -4;
  135. }
  136. // if(z>1) return -4;
  137. return 100;
  138. }
  139. int main(){
  140. boost
  141. int t;
  142. cin>>t;
  143. while(t--){
  144. int n;
  145. cin>>n;
  146. int B[n+5];
  147. n--;
  148. read(B,n);
  149. if(n==1){
  150. ll ans=(get(0,B[0])+get(B[0],0)-1+mod)%mod;
  151. cout<<ans<<endl;
  152. continue;
  153. }
  154. int x=order(B,n);
  155. // log(x);
  156. ll ans=0;
  157. if(x==-3 or x==-4){
  158. cout<<0<<endl;
  159. continue;
  160. }
  161. else{
  162. sort(B,B+n,greater<int>());
  163. ll p=0;
  164. int i=0;
  165. while(i<n and B[i]==B[0]) i++,p++;
  166. ans+=(1LL*(p+1)*(get(B[0],B[0])-1))%mod;
  167. ans=(ans+1)%mod;
  168. cout<<ans<<endl;
  169. }
  170. }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment