#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000000;
const long long MOD = 998244353;
long long qpow(long long a, long long p) {
  long long res = 1, base = a;
  while (p) {
    if (p & 1) res = res * base % MOD;
    p >>= 1;
    base = base * base % MOD;
  }
  return res;
}
long long mod_inv(long long x) { return qpow(x, MOD - 2); }
long long fac[MAXN + 5], ifac[MAXN + 5];
long long choose(long long n, long long k) {
  return fac[n] * ifac[k] % MOD * ifac[n - k] % MOD;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long N;
  cin >> N;
  fac[0] = ifac[0] = 1;
  for (int i = 1; i <= N; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    ifac[i] = mod_inv(fac[i]);
  }
  long long sum1 = 0;
  for (long long i = 1; i <= N; i++) {
    sum1 += choose(N, i) * qpow(-1, i + 1) % MOD * qpow(3, i + N * (N - i)) *
            2 % MOD;
    sum1 %= MOD;
  }
  long long sum2 = 0;
  for (int i = 1; i <= N; i++) {
    sum2 += 3 * qpow(-1, i + 1) % MOD * choose(N, i) % MOD *
            (qpow(qpow(3, N - i) - 1, N) - qpow(3, N * (N - i))) % MOD;
    sum2 %= MOD;
  }
  cout << ((sum1 + sum2) % MOD + MOD) % MOD << "\n";
}
