#include <bits/stdc++.h>
using namespace std;
template <class T>
void read(T &u) {
  u = 0;
  char c = getchar();
  long long flag = 0;
  while (c < '0' || c > '9') flag |= (c == '-'), c = getchar();
  while (c >= '0' && c <= '9')
    u = (u << 3) + (u << 1) + (c ^ 48), c = getchar();
  if (flag) u = -u;
}
template <class T>
bool checkmin(T &a, T b) {
  return (b < a ? a = b, 1 : 0);
}
template <class T>
bool checkmax(T &a, T b) {
  return (b > a ? a = b, 1 : 0);
}
template <class T>
T _min(T &a, T &b) {
  return (a < b ? a : b);
}
template <class T>
T _max(T &a, T &b) {
  return (a > b ? a : b);
}
const long long mod = 998244353, N = 2000005;
long long n, t, ans = 0;
long long fac[N], inv_fac[N], P[N];
long long qpow(long long a, long long b) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans = ans * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ans;
}
long long C(long long a, long long b) {
  return fac[a] * inv_fac[a - b] % mod * inv_fac[b] % mod;
}
void init() {
  read(n);
  fac[0] = 1LL;
  for (long long i = 1; i <= n; ++i) {
    fac[i] = fac[i - 1] * i % mod;
  }
  inv_fac[n] = qpow(fac[n], mod - 2) % mod;
  for (long long i = n; i >= 1; --i) {
    inv_fac[i - 1] = inv_fac[i] * (i) % mod;
  }
  P[0] = 1LL;
  for (long long i = 1; i <= n; ++i) {
    P[i] = P[i - 1] * 3 % mod;
  }
  ans = (qpow(3, n * n) - qpow(P[n] + mod - 3, n) + mod) % mod;
}
void solve() {
  for (long long i = 1; i <= n; ++i) {
    long long num;
    num = (3 * C(n, i) * qpow(P[n - i] + mod - 1, n) % mod) % mod;
    num =
        (num + C(n, i) * qpow(3, n * (n - i)) % mod * (P[i] + mod - 3) % mod) %
        mod;
    if (i % 2 == 1) {
      ans = (ans + num) % mod;
    } else {
      ans = (ans - num + mod) % mod;
    }
  }
  printf("%lld\n", ans);
}
int main() {
  init();
  solve();
  return 0;
}
